Technology Exchange Group: 233513714
/*** AES Encryption Test * *@paramSTR Encryption Parameters*/ Public voidaestest (String str) {Log.info ([Rsatest GET request: {}] ", str); Try{String Encryptresult= Aesutil.encrypt (str, "123456"); Log.info ("[AES Encrypted parameters are: {}]", Encryptresult); String Decryptresult= Aesutil.decrypt (Encryptresult, "123456"); Log.info ("[AES decrypted parameters are: {}]", Decryptresult); } Catch(Exception e) {log.info ("[AES Encryption decryption exception occurred: {}]", E); } } /*** AES Encryption * *@paramcontent needs to be encrypted *@paramPassword encrypted password *@return */ Public Staticstring Encrypt (string content, string password) {Try{keygenerator KGen= Keygenerator.getinstance ("AES"); Kgen.init (128,NewSecureRandom (Password.getbytes ())); Secretkey Secretkey=Kgen.generatekey (); byte[] Encodeformat =secretkey.getencoded (); Secretkeyspec Key=NewSecretkeyspec (Encodeformat, "AES"); Cipher Cipher= Cipher.getinstance ("AES"); byte[] bytecontent = Content.getbytes ("Utf-8"); Cipher.init (Cipher.encrypt_mode, key); byte[] result =cipher.dofinal (bytecontent); BASE64 Base64=NewBase64 (); returnbase64.encodetostring (Result); } Catch(Exception e) {log.info ("AES Encryption exception occurred: {}", E); } return NULL; } /*** AES Decryption * *@paramcontent to decrypt contents *@paramPassword decryption key *@return */ Public Staticstring Decrypt (string content, string password) {Try{Base64 base64=NewBase64 (); byte[] Text =Base64.decode (content); Keygenerator KGen= Keygenerator.getinstance ("AES"); Kgen.init (128,NewSecureRandom (Password.getbytes ())); Secretkey Secretkey=Kgen.generatekey (); byte[] Encodeformat =secretkey.getencoded (); Secretkeyspec Key=NewSecretkeyspec (Encodeformat, "AES"); Cipher Cipher= Cipher.getinstance ("AES"); Cipher.init (Cipher.decrypt_mode, key); byte[] result =cipher.dofinal (text); return NewString (Result); } Catch(Exception e) {log.info ("AES Decryption exception occurred: {}", E); } return NULL; }
Decryption using AES encryption in Java