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