CryptoJS 加密

加密代码

var orgStr=123456;

//var token=“262ccaffda429ae06c05df9a334aad1e”;

function encode(orgStr, token) {

var key = CryptoJS.enc.Utf8.parse(token.substr(0, 16));

var iv = CryptoJS.enc.Utf8.parse(token.substr(15, 16));

var srcs = CryptoJS.enc.Utf8.parse(orgStr);

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

return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);

}


参考解密代码:

var token="262ccaffda429ae06c05df9a334aad1e";

function unencode(mima) {

var key = CryptoJS.enc.Utf8.parse(token.substr(0, 16));

var iv = CryptoJS.enc.Utf8.parse(token.substr(15, 16));

var baseResult=CryptoJS.enc.Base64.parse(mima);  // Base64解密

var decryptResult = CryptoJS.AES.decrypt(baseResult,key, {    //  AES解密

        iv: iv,

        mode: CryptoJS.mode.CBC,

        padding: CryptoJS.pad.Pkcs7

    });

      let resData=decryptResult.toString(CryptoJS.enc.Utf8).toString();

    return JSON.parse(resData);

}

你可能感兴趣的:(CryptoJS 加密)