先下載CryptoJS並引入
1、Hex格式:
const key = CryptoJS.enc.Utf8.parse("cmp_security_key");
const options = {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}
加密
const encryptedStr = CryptoJS.AES.encrypt("222", key, options).ciphertext.toString();
console.log(encryptedStr);
解密
const testdec = CryptoJS.AES.decrypt(CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse(encryptedStr)), key, options).toString(CryptoJS.enc.Utf8);
console.log(testdec);
2、Base64
加密
const encryptedStr1 = CryptoJS.AES.encrypt("222", key, options).toString();
console.log(encryptedStr1);
解密
const testdec1 = CryptoJS.AES.decrypt(encryptedStr1, key, options).toString(CryptoJS.enc.Utf8);
console.log(testdec1);