https://github.com/sytelus/CryptoJS
其中 CryptoJS 使用的谷歌开源 https://code.google.com/p/crypto-js/
https://code.google.com/archive/p/crypto-js/downloads
AES对称加密,比DES安全性高。
AES分为ECB和CBC两种方式。
CBC模式比ECB模式安全。
ECB模式比CBC模式快。
CBC加密解密方式
下载完成后在页面中引入
rollups/aes.js
components/pad-zeropadding.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>aes</title>
<script src="aes.js"></script>
<script src="pad-zeropadding.js"></script>
</head>
<body>
<script type="text/javascript">
CBC();
function CBC() {
var key = "0000000671595991";
var iv = "tdrdadq59tbss5n7";
var pazzword = '123456';
console.log('pazzword:' + pazzword);
// aes 加密
pazzword = encrypt(pazzword, key, iv);
console.log('加密后:' + pazzword);
if (pazzword.length == 24) {
pazzword = decrypt(pazzword, key, iv);
console.log('解密后:' + pazzword);
}
}
// 加密
function encrypt(data, key, iv) { //key,iv:16位的字符串
var key1 = CryptoJS.enc.Latin1.parse(key);
var iv1 = CryptoJS.enc.Latin1.parse(iv);
return CryptoJS.AES.encrypt(data, key1, {
iv: iv1,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
}).toString();
}
// 解密
function decrypt(data, key, iv) { //key,iv:16位的字符串
var key1 = CryptoJS.enc.Latin1.parse(key);
var iv1 = CryptoJS.enc.Latin1.parse(iv);
var decrypted = CryptoJS.AES.decrypt(data, key1, {
iv: iv1,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
</script>
</body>
</html>
ECB加密解密方式
下载完成后在页面中引入
rollups/aes.js
components/mode-ecb.js
components/pad-zeropadding.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>aes</title>
<script src="aes.js"></script>
<script src="mode-ecb.js"></script>
<script src="pad-zeropadding.js"></script>
</head>
<body>
<script type="text/javascript">
ECB();
function ECB() {
var key = "0000000671595991";
var iv = "tdrdadq59tbss5n7";
var pazzword = '123456';
console.log('pazzword:' + pazzword);
// aes 加密
pazzword = encrypt(pazzword, key, iv);
console.log('加密后:' + pazzword);
if (pazzword.length == 24) {
pazzword = decrypt(pazzword, key, iv);
console.log('解密后:' + pazzword);
}
}
// 加密
function encrypt(data, key, iv) { //key,iv:16位的字符串
var key1 = CryptoJS.enc.Latin1.parse(key);
var iv1 = CryptoJS.enc.Latin1.parse(iv);
return CryptoJS.AES.encrypt(data, key1, {
iv: iv1,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.ZeroPadding
}).toString();
}
// 解密
function decrypt(data, key, iv) { //key,iv:16位的字符串
var key1 = CryptoJS.enc.Latin1.parse(key);
var iv1 = CryptoJS.enc.Latin1.parse(iv);
var decrypted = CryptoJS.AES.decrypt(data, key1, {
iv: iv1,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.ZeroPadding
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
</script>
</body>
</html>
Java实现方式
AES对称加密_小百菜的博客-CSDN博客
注意修改代码中的填充方式:
AES/CBC/PKCS5Padding 改为 AES/CBC/NoPadding
AES/ECB/PKCS5Padding 改为 AES/ECB/NoPadding
在线测试
在线测试http://tool.chacuo.net/cryptaes