Decryption using AES encryption in Java

Source: Internet
Author: User
Tags decrypt

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

Related Article

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.