js AES加密 转 Base64 再转成urlEncode
安装 crypto-js 项目地址:https://github.com/lzx-cloud/examples-h5/tree/main/aes
运行命令: cnpm install crypto-js --save
新建脚本 tool.js
const CryptoJS = require('crypto-js'); //引用AES源码js var key = CryptoJS.enc.Utf8.parse("123456"); var iv = CryptoJS.enc.Utf8.parse("T(123456"); //解密方法 function Decrypt(word) { let encryptedHexStr = CryptoJS.enc.Base64.parse(word); let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); } //加密方法 function Encrypt(word){ let srcs = CryptoJS.enc.Utf8.parse(word); let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); let encryptedHexStr = CryptoJS.enc.Base64.stringify(encrypted.ciphertext); return encryptedHexStr.toString(); // return encrypted.ciphertext.toString().toUpperCase(); //不需要加密base64的时候可以直接返回 } exports.Decrypt = Decrypt; exports.Encrypt = Encrypt;
新建脚本 然后引入tool.js即可
const {Decrypt,Encrypt} = require('./tools'); const en = Encrypt("20211109134330"); console.dir("加密后:" + en); const de = Decrypt('TATKPwbeKuoV/SqsYb0cfw=='); console.dir("解密后:" + de); console.dir("---------"); console.dir(encodeURIComponent ("N8c15TVp+CIAQ5I3IHZQZw==")); console.dir(decodeURIComponent("N8c15TVp%2BCIAQ5I3IHZQZw%3D%3D")); console.dir("---------"); console.dir(encodeURIComponent ("TATKPwbeKuoV/SqsYb0cfw==")); console.dir(decodeURIComponent("TATKPwbeKuoV%2FSqsYb0cfw%3D%3D"));