crypto AES 加密 解密
1、aes加密解密说明
https://juejin.im/entry/59eea48e6fb9a0451968c25f
aes是对称加密算法
md5是摘要算法,不算是加密,主要用来对信息一致性和完整性校验。是不可逆的。
2、示例代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>crypto 加密解密算法</title>
</head>
<body>
<script src="https://cdn.bootcss.com/crypto-js/3.1.9/crypto-js.min.js"></script>
<script type="text/javascript">
//加密
function encrypt(content, key) {
if(typeof content === 'object') {
return CryptoJS.AES.encrypt(JSON.stringify(content), key).toString()
}
return CryptoJS.AES.encrypt(content.toString(), key).toString()
}
//解密
function decrypt(content, key) {
try {
return JSON.parse(CryptoJS.AES.decrypt(content, key).toString(CryptoJS.enc.Utf8))
} catch(e) {
return CryptoJS.AES.decrypt(content, key).toString(CryptoJS.enc.Utf8)
}
}
console.log(encrypt('abcabcabcabcabcabcabcabcabcabcabcabcabc', 'mfg'))
console.log(decrypt(encrypt('abcabcabcabcabcabcabcabcabcabcabcabcabc', 'mfg'), 'mfg'))
</script>
</body>
</html>
作者:孟繁贵 Email:meng010387@126.com 期待共同进步!