//秘钥
const CRYPTOJSKEY = "xxxxxxxxxxxx";
// 加密
function encrypt(plaintText) {
var plaintText = plaintText;
var options = {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
};
var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
var encryptedData = CryptoJS.AES.encrypt(plaintText, key, options);
var encryptedBase64Str = encryptedData.toString();
return encryptedBase64Str;
}
//解密
function decrypt(encryptedBase64Str) {
var vals = encryptedBase64Str;
var options = {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
};
var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
var decryptedData = CryptoJS.AES.decrypt(vals, key, options);
var decryptedStr = CryptoJS.enc.Utf8.stringify(decryptedData);
return decryptedStr
}
注:发送网络请求是加密字符串中的'+'字符会被转义(encodeURIComponent 方法解决)
例:encodeURIComponent(encrypt(JSON.stringify(params))) //params 需要加密的对象