安装
npm install crypto-js
github: https://github.com/brix/crypto-js
简单封装
const CryptoJS = require("crypto-js"); // node环境下导入模块// import CryptoJS from "crypto-js";
// Encrypt 加密
function encrypt(key, text){
return CryptoJS.AES.encrypt(text, key).toString();
}
// Decrypt 解密
function decrypt(key, cipherText){
let bytes = CryptoJS.AES.decrypt(cipherText, key);
return bytes.toString(CryptoJS.enc.Utf8);
}
使用示例
let key = 'thisiskey'
let text = '你好'
let cipherText = encrypt(key, text);
console.log(cipherText)
// U2FsdGVkX1/G3fZMUdKiN6UMmqnIfdQsM3b1Ff6o5Pk=
let originalText = decrypt(key, cipherText)
console.log(originalText)
// 你好
参考
</div>