js AES+ECB加密

项目要实现请求参数加密

1.aes.js引入



2.加密解密方法调用

// AES加密方法,encrypt:要加密的数据
 function aesEncrypt (encrypt) {

     var aesPrivateKey = '1234567890123456';//密钥

     var key = CryptoJS.enc.Utf8.parse(aesPrivateKey);

     var srcs = CryptoJS.enc.Utf8.parse(JSON.stringify(encrypt));

     var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});

     return encrypted.toString();
 }
 
 // AES 解密方法
function aesDecrypt (decryptString) {

     var aesPrivateKey = '1234567890123456';//密钥

     var key = CryptoJS.enc.Utf8.parse(aesPrivateKey);

     var decrypt = CryptoJS.AES.decrypt(decryptString, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});

     return CryptoJS.enc.Utf8.stringify(decrypt).toString();
 }
//如何调用?举例
 var data1 = {yhbh:'2018048'};

 var AES = aesEncrypt(data1);

 console.log(AES);//klOJHhgVNMS1fyvMv/AWZVvvlIl5yWe26nXZSEFU6Bc=

 var AFTERAES = aesDecrypt(AES);

 console.log(AFTERAES);//{yhbh:'2018048'};

1578377873756.jpg

搞定~

你可能感兴趣的:(javascript,aes)