<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AES加密解密</title>
<script src="./crypto-js.js"></script>
</head>
<body>
<script>
console.log("CryptoJS---->", CryptoJS)
// AES加密
function encryptByAES(word, keyStr) {
keyStr = keyStr ? keyStr : "kaimo313";
let key = CryptoJS.enc.Utf8.parse(keyStr);
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
return encrypted.toString();
}
// AES解密
function decryptByAES(word, keyStr) {
keyStr = keyStr ? keyStr : "kaimo313";
let key = CryptoJS.enc.Utf8.parse(keyStr);
let decrypt = CryptoJS.AES.decrypt(word, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
console.log("使用 key:kaimo313 对密码 123456 进行加密", encryptByAES("123456"));
console.log("使用 key:kaimo313 对密码 123456 进行解密", decryptByAES(encryptByAES("123456")));
</script>
</body>
</html>