Two AES encryption methods of JavaScript (which can be encrypted and decrypted with Java)

  • 2021-07-07 06:05:15
  • OfStack

Because JavaScript is a weakly typed scripting language, there will be various problems when it interacts with strongly typed background languages, especially encryption and decryption operations. Because I encounter the problem of encryption and decryption with js and Java in my work, I have searched a lot of data and code segments on the Internet, but they can't be solved. After summarizing the contents of many documents, I finally found a solution, which is now recorded here:

Here are two AES encryption methods for JavaScript. The specific details are as follows:

Type 1: When the secret key (key) and the secret key offset (iv) are needed for encryption and decryption, the online verification address is http://www.seacha.com/tools/aes. html


// The method can be associated with Java Carry out mutual encryption and decryption <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> A secret key is required ( key ) and the key offset ( iv ) aes Encryption and decryption </title>
</head>
<body>
<script src="aes_1.js"></script>    // Introduced js The file is in the link: https://github.com/hellobajie/AES-of-JavaScript
<script>
var key = CryptoJS.enc.Utf8.parse("106 Bit 106 Binary number as secret key "); 
var iv = CryptoJS.enc.Utf8.parse('106 Bit 106 Binary number as secret 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 each step, you can refer to: http://zhidao.baidu.com/question/647688575019014285.html?qbl=relate_question_0&word=javascript%20aes 

Type 2: Only the secret key is needed for encryption and decryption, and the online verification address is http://encode.chahuo.com/


<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Only the secret key is needed for encryption and decryption </title>
</head>
<body>
<script src="aes_2.js"></script>    // Introduced js The file is in the link: https://github.com/hellobajie/AES-of-JavaScript
<script type="text/javascript">
var pwd=" Secret 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>

Related articles: