Because JavaScript is a weakly typed scripting language, there are a variety of problems when it interacts with strongly typed background languages, especially when it comes to decrypting operations. I encountered in the work with JS and Java to decrypt each other, in the online search a lot of information and code snippets, can not be solved. After summing up a number of document content finally found a solution, is now recorded with this:
The first: When encryption and decryption require a key (key) and key offset (iv), online authentication address: http://www.seacha.com/tools/aes.html
This method can be decrypted with Java
<!doctype html>varKey = CryptoJS.enc.Utf8.parse ("16-bit hexadecimal number as the secret key"); varIV = CryptoJS.enc.Utf8.parse (' 16 hexadecimal digits as key offset ')); functionEncrypt (Word) {SRCs=CryptoJS.enc.Utf8.parse (word); varencrypted =CryptoJS.AES.encrypt (SRCs, key, {IV:IV,MODE:CRYPTOJS.MODE.CBC,PADDING:CRYPTOJS.PAD.PKCS7}); returnencrypted.ciphertext.toString (). toUpperCase (); } functionDecrypt (word) {varEncryptedhexstr =CryptoJS.enc.Hex.parse (word); varSRCs =CryptoJS.enc.Base64.stringify (ENCRYPTEDHEXSTR); varDecrypt =CryptoJS.AES.decrypt (SRCs, key, {IV:IV,MODE:CRYPTOJS.MODE.CBC,PADDING:CRYPTOJS.PAD.PKCS7}); varDecryptedstr =decrypt.tostring (CryptoJS.enc.Utf8); returndecryptedstr.tostring (); } varmm = Encrypt (' Nihao ') Console.log (mm); varJM =Decrypt (mm); Console.log (JM)</script></body>
If you want to understand each step in depth, you can refer to: http://zhidao.baidu.com/question/647688575019014285.html?qbl=relate_question_0&word= Javascript%20aes
The second: encryption and decryption only need the secret key, online authentication address: http://encode.chahuo.com/
<!doctype html>varPwd= "Secret Key"; functionEncrypt (word) {returnCryptoJS.AES.encrypt (word,pwd). toString (); } functionDecrypt (word) {returnCryptoJS.AES.decrypt (word,pwd). toString (CryptoJS.enc.Utf8); } varmm = Encrypt (' Nihao '); Console.log (mm)varJM =Decrypt (mm); Console.log (JM)</script></body>Two types of JavaScript AES encryption (can be decrypted with Java)