function encryptByAES(plainText, keyInBase64Str, ivInBase64Str) {
let key = CryptoJS.enc.Base64.parse(keyInBase64Str)
let iv = CryptoJS.enc.Base64.parse(ivInBase64Str)
let encrypted = CryptoJS.AES.encrypt(plainText, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return encrypted.ciphertext.toString(CryptoJS.enc.Base64)
}
function decryptByAES(cipherText, keyInBase64Str, ivInBase64Str) {
let key = CryptoJS.enc.Base64.parse(keyInBase64Str)
let iv = CryptoJS.enc.Base64.parse(ivInBase64Str)
let decrypted = CryptoJS.AES.decrypt(cipherText, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return decrypted.toString(CryptoJS.enc.Utf8)
}
let secret = 'sQPoC/1do9BZMkg8I5c09A=='
let cipherText = encryptByAES('123456', secret, secret)
let plainText = decryptByAES(cipherText, secret, secret)
console.log('123456加密后的密文为:' + cipherText)
console.log('解密后的内容为:' + plainText)
console