Loading... ### aes加密 ```js function encrypt(word) { var key = CryptoJS.enc.Utf8.parse("1234567890000000") //16位密钥 var iv = CryptoJS.enc.Utf8.parse("1234567890000000") // 偏移量 var encrypted = '' if (typeof(word) == 'string') { var srcs = CryptoJS.enc.Utf8.parse(word) encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); } else if (typeof(word) == 'object') {//对象格式的转成json字符串 data = JSON.stringify(word); var srcs = CryptoJS.enc.Utf8.parse(data); encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }) } return encrypted.ciphertext.toString() } ``` ### aes解密 ```js function decrypt(word) { var key = CryptoJS.enc.Utf8.parse("1234567890000000") //16位密钥 var iv = CryptoJS.enc.Utf8.parse("1234567890000000") // 偏移量 var encryptedHexStr = CryptoJS.enc.Hex.parse(word) var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr) var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }) var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8) return decryptedStr.toString() } ``` ## ps:严格注意密钥长度 最后修改:2022 年 12 月 07 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏