Two types of JavaScript AES encryption (can be decrypted with Java)

Source: Internet
Author: User
Tags decrypt pkcs7

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)

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.