AES encryption and decryption between Java background and front-end JavaScript

Source: Internet
Author: User
Tags base64 decrypt

Debugging a day Finally, the Java background and JavaScript between the AES encryption decryption successful, record the process.

Background Java decryption code: decoding algorithm and mode for

Aes/cbc/pkcs5padding

Key and IV to be 16-bit

Get 16 of the character array converted to a string according to the 16 binary encoding

 Public StaticString Encrypt (string content, string key)throwsException {Try{Key KeySpec=NewSecretkeyspec (Key.getbytes (), "AES");//two parameters, the first is the private key byte array, the second one is encrypted Aes or desString IV= "1234567890123456";//initialize the vector parameter, AES is 16bytes. DES is 8bytes.Ivparameterspec Ivspec=NewIvparameterspec (Iv.getbytes ()); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding");            Cipher.init (Cipher.encrypt_mode, Keyspec,ivspec); //Cipher Cipher = Aesutil.generatecipher (Cipher.encrypt_mode, "1234567890123456". GetBytes (), "1234567890123456". GetBytes ());            byte[] Byteresult =cipher.dofinal (Content.getbytes ()); StringBuffer SB=NewStringBuffer ();  for(inti = 0; i < byteresult.length; i++) {String hex= Integer.tohexstring (Byteresult[i] & 0xFF); if(hex.length () = = 1) {hex= ' 0 ' +Hex;            } sb.append (Hex.touppercase ()); }            returnsb.tostring (); } Catch(Exception e) {e.printstacktrace (); }        return NULL; }

The front-end JavaScript code is as follows:

37999d4237a3d2701eb368ec53097ec963b5e128d13c8360051ece37af7d3a649626aa1d828b9b61813a7557c2c464cdfd913d84c8e507804b51b5904 323C8BF ciphertext generated for Java background

You need to convert to a 16-byte array before being converted to BASE64 encoding to be
The CryptoJS.AES.decrypt method is used.

Key and IV also need to be transformed by CryptoJS.enc.Utf8.parse method to be used.
The resulting decrypted1 is an object that needs to be parsed before it can be used.

The dependent JS file is Cryptojs v3.1.2:<script type= "Text/javascript" src= "Js/aes.js" ></script>
        varEncryptedhexstr = CryptoJS.enc.Hex.parse (" 37999d4237a3d2701eb368ec53097ec963b5e128d13c8360051ece37af7d3a649626aa1d828b9b61813a7557c2c464cdfd913d84c8e507804b51b5904 323C8BF ");         //string to convert ciphertext to Base64        //only a string cipher of type Base64 can decrypt it        varEncryptedbase64str =CryptoJS.enc.Base64.stringify (ENCRYPTEDHEXSTR); //alert ("Encryptedbase64str");        vardecrypted1 = CryptoJS.AES.decrypt (Encryptedbase64str, CryptoJS.enc.Utf8.parse ("1234567890123456"), {iv:CryptoJS.enc.Utf8.parse ("1234567890123456"), Mode:CryptoJS.mode.CBC, PADDING:CRYPTOJS.PAD.PKCS7});        Alert (CryptoJS.enc.Utf8.stringify (decrypted1). toString ()); vartxt = (CryptoJS.enc.Utf8.stringify (decrypted). ToString ());

AES encryption and decryption between Java background and front-end JavaScript

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.