crypto-js使用 :
const cryptoJs = require('crypto-js')
function getKey(key = "1234123412341234") {
return cryptoJs
.SHA1(cryptoJs.SHA1(key))
.toString()
.substring(0, 32);
}
function aesEncrypt(data, key) {
console.log('加密之前的data:', data+' key:'+ key + ' 真实的key:'+ getKey(key));
const KEY = cryptoJs.enc.Hex.parse(getKey(key));
const IV = cryptoJs.enc.Hex.parse(getKey(key));
if (typeof data === "object") {
try {
data = JSON.stringify(data);
} catch (error) {
console.log("加密失败:", error);
}
}
let encrypt = cryptoJs.AES.encrypt(
data,
KEY,
{
iv: IV,
mode: cryptoJs.mode.ECB,
padding: cryptoJs.pad.Pkcs7
}
);
console.log("加密之后的数据:", encrypt.ciphertext.toString());
return encrypt.ciphertext.toString();
}
function aesDecrypt(data, key) {
console.log('解密之前的data:', data+' key:'+ key + ' 真实的key:'+ getKey(key));
const KEY = cryptoJs.enc.Hex.parse(getKey(key));
const IV = cryptoJs.enc.Hex.parse(getKey(key));
let str = cryptoJs.enc.Base64.stringify(cryptoJs.enc.Hex.parse(data));
let decrypt = cryptoJs.AES.decrypt(
str,
KEY,
{
iv: IV,
mode: cryptoJs.mode.ECB,
padding: cryptoJs.pad.Pkcs7
}
);
console.log('解密后的数据:', decrypt.toString(cryptoJs.enc.Utf8).toString())
return decrypt.toString(cryptoJs.enc.Utf8);
}
let obj = {
name: '狠人大帝'
}
let a = aesEncrypt(obj, '45674567456745674567')
console.log('加密后的a====', a);
let a1 = aesDecrypt(a, '45674567456745674567')
console.log('解密后的a1', a1)
// export { aesEncrypt, aesDecrypt };