Since JavaScript is a weak-type scripting language, various problems arise when it interacts with a strong-type background language, especially the encryption and decryption operations. Because I encountered the problem of using js and Java to encrypt and decrypt each other during my work, I searched a lot of information and code segments online, but they could not be solved. After summarizing the content of multiple documents, I finally found a solution. I will record it as follows:
Here are two JavaScript AES encryption methods. The specific details are as follows:
The first type: When the secret key (key) and the secret key offset (iv) are required during encryption and decryption, the online verification address is: http://www.seacha.com/tools/aes.html
//This method can be mutually encrypted and decrypted with Java<!doctype html><html lang="en"><head><meta charset="UTF-8"><title>Aes encryption and decryption of the secret key (key) and the secret key offset (iv)</title></head><body><script src="aes_1.js"></script> //The imported js file is in this link: https://github.com/hellobajie/AES-of-JavaScript<script>var key = CryptoJS.enc.Utf8.parse("Sixteen-bit hexadecimal number as the secret key"); var iv = CryptoJS.enc.Utf8.parse('Hext-bit hexadecimal number as key offset'); function Encrypt(word){srcs = CryptoJS.enc.Utf8.parse(word);var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7});return encrypted.ciphertext.toString().toUpperCase();}function Decrypt(word){ var encryptedHexStr = CryptoJS.enc.Hex.parse(word);var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7});var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString();}var mm = Encrypt('nihao')console.log(mm);var jm = Decrypt(mm);console.log(jm)</script></body></html>//If you want to have a deep understanding of the function of each step, you can refer to: http://zhidao.baidu.com/question/647688575019014285.html?qbl=relate_question_0&word=javascript%20aesThe second type: only a secret key is required when encrypting and decrypting, and the online verification address is: http://encode.chahuo.com/
<!doctype html><html lang="en"><head><meta charset="UTF-8"><title>Only a secret key is required when encrypting and decrypting</title></head><body><script src="aes_2.js"></script> //The imported js file is in this link: https://github.com/hellobajie/AES-of-JavaScript<script type="text/javascript">var pwd="key";function Encrypt(word){return CryptoJS.AES.encrypt(word,pwd).toString();}function Decrypt(word){return CryptoJS.AES.decrypt(word,pwd).toString(CryptoJS.enc.Utf8);}var mm = Encrypt('nihao');console.log(mm)var jm = Decrypt(mm);console.log(jm)</script></body></html>The above are the two JavaScript AES encryption methods (can be mutually encrypted and decrypted with Java) introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!