AES Plus decryption in Java

Source: Internet
Author: User
Tags base64

Directly on the code, BASE64 using the method of Java8, if not, replace can

KEY: That is the password

IV: Offset, customizable, 16-bit

Encryption method: aes/cbc/pkcs5padding,128 bit encryption

If you want to use 256-bit and pkcs7padding to import the package additional

ImportJavax.crypto.*;ImportJavax.crypto.spec.IvParameterSpec;ImportJavax.crypto.spec.SecretKeySpec;Importjava.security.InvalidAlgorithmParameterException;Importjava.security.InvalidKeyException;Importjava.security.NoSuchAlgorithmException;ImportJava.security.SecureRandom;Importjava.util.Base64;/*** Add Decryption tool * *@authorHackyo * Created on 2017/12/13 18:33.*/ Public Final classEncrypt {Private Static FinalString cbc_cipher_algorithm = "Aes/cbc/pkcs5padding"; Private Static FinalString KEY = "AAAAAAAA"; Private Static FinalString IV = "abcdefghijklm123"; Private StaticSecretkey Secretkey; Static {        Try{keygenerator Keygenerator= Keygenerator.getinstance ("AES"); Keygenerator.init (128,NewSecureRandom (Key.getbytes ())); Secretkey=NewSecretkeyspec (Keygenerator.generatekey (). getencoded (), "AES"); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args) {String original= "Encrypt Me"; String Encodedtext=Aesencode (original); System.out.println ("Ciphertext (ciphertext after Base64 encryption)" +encodedtext); System.out.println ("Original (decrypted)" +Aesdecode (Encodedtext)); }    /*** AES Encryption * *@paramOriginal Original *@returnCiphertext*/     Public Staticstring Aesencode (string original) {Try{Cipher Cipher=cipher.getinstance (Cbc_cipher_algorithm); Cipher.init (Cipher.encrypt_mode, Secretkey,NewIvparameterspec (Iv.getbytes ())); returnBase64.getencoder (). Encodetostring (Cipher.dofinal (Original.getbytes ())); } Catch(NoSuchAlgorithmException | nosuchpaddingexception | InvalidKeyException | invalidalgorithmparameterexception | illegalblocksizeexception |badpaddingexception e)        {E.printstacktrace (); }        return NULL; }    /*** AES Decryption * *@paramCiphertext ciphertext *@returnOriginal*/     Public Staticstring Aesdecode (String ciphertext) {Try{Cipher Cipher=cipher.getinstance (Cbc_cipher_algorithm); Cipher.init (Cipher.decrypt_mode, Secretkey,NewIvparameterspec (Iv.getbytes ())); return NewString (Cipher.dofinal (Base64.getdecoder (). Decode (ciphertext))); } Catch(NoSuchAlgorithmException | nosuchpaddingexception | InvalidKeyException | invalidalgorithmparameterexception | illegalblocksizeexception |badpaddingexception e)        {E.printstacktrace (); }        return NULL; }}

AES Plus decryption in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.