前端加密传输 crypto-js AES 加密和解密
程序员文章站
2022-05-17 14:49:41
配置: 配置: ......
配置:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>document</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>
<script>
function getaesstring(data, key, iv) { //加密
var key = cryptojs.enc.utf8.parse(key);
var iv = cryptojs.enc.utf8.parse(iv);
var encrypted = cryptojs.aes.encrypt(data, key, {
iv: iv,
mode: cryptojs.mode.cbc,
padding: cryptojs.pad.pkcs7
});
return encrypted.tostring(); //返回的是base64格式的密文
}
function getdaesstring(encrypted, key, iv) { //解密
var key = cryptojs.enc.utf8.parse(key);
var iv = cryptojs.enc.utf8.parse(iv);
var decrypted = cryptojs.aes.decrypt(encrypted, key, {
iv: iv,
mode: cryptojs.mode.cbc,
padding: cryptojs.pad.pkcs7
});
return decrypted.tostring(cryptojs.enc.utf8);
}
function getaes(data) { //加密
var key = '12345678910asdfx'; //密钥
var iv = '12345678910asdfx';
var encrypted = getaesstring(data, key, iv); //密文
var encrypted1 = cryptojs.enc.utf8.parse(encrypted);
return encrypted;
}
function getdaes(data) { //解密
var key = '12345678910asdfx'; //密钥
var iv = '12345678910asdfx'; //密钥偏移量
var decryptedstr = getdaesstring(data, key, iv);
return decryptedstr;
}
var aes = getaes('hello');
var afteraes = getdaes(aes);
console.log('加密后:' + aes );
console.log('解密后:' + afteraes );
</script>
</body>
</html>
上一篇: JQuery Highcharts 动态生成图表的方法
下一篇: 控制语句
推荐阅读