// const keyStr ='0123456789ABCDFB';
// const ivStr = 'FEDCBA9876543210'
const keyStr ='0123456789ABCDEF';
const ivStr = 'FEDCBA9876543210'
/**
* AES-CBC对称加密
* @param text {string} 要加密的明文
* @param secretKey {string} 密钥,十六进制字符串
* @returns {string} 加密后的密文,Base64格式
*/
export const cryptoEncrypt = (word) => {
var key = CryptoJS.enc.Utf8.parse(keyStr);
var iv = CryptoJS.enc.Utf8.parse(ivStr);
var srcs = ''
if (typeof (word) == 'string') {
srcs = CryptoJS.enc.Utf8.parse(word);
} else if (typeof (word) == 'object') {
srcs = CryptoJS.enc.Utf8.parse(JSON.stringify(word));
}
var encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
// return encrypted.ciphertext.toString(); //hex字符串的形式
return encrypted.toString(); //base64的形式
}
/**
* AES-CBC对称解密
* @param textBase64 {string} 要解密的密文,Base64格式
* @param secretKey {string} 密钥 十六进制字符串
* @returns {string} 解密后的明文
*/
export const cryptoDecrypt = (textBase64) => {
var key = CryptoJS.enc.Utf8.parse(keyStr);
var iv = CryptoJS.enc.Utf8.parse(ivStr);
var decrypt = CryptoJS.AES.decrypt(textBase64, key, {
"mode": CryptoJS.mode.CBC,
"iv": iv,
"padding": CryptoJS.pad.Pkcs7
});
return CryptoJS.enc.Utf8.stringify(decrypt);
}
const EncryptStr = cryptoEncrypt("admin")
console.log('Encrypt str is ' + EncryptStr)
console.log('Decrypt str is ' + cryptoDecrypt(EncryptStr))
console