function getAesString(data,key,iv){//加密
    var key  = CryptoJS.enc.Latin1.parse(key);
    var iv   = CryptoJS.enc.Latin1.parse(iv);
    var srcs = CryptoJS.enc.Utf8.parse(data);
 
    var encrypted = CryptoJS.AES.encrypt(srcs,key,
                                         {
                                         iv:iv,
                                         mode:CryptoJS.mode.CBC,
                                         padding:CryptoJS.pad.Pkcs7
                                         });
    return encrypted;
}
function getDAesString(encrypted,key,iv){//解密
    var key  = CryptoJS.enc.Latin1.parse(key);
    var iv   = CryptoJS.enc.Latin1.parse(iv);
    var decrypted = CryptoJS.AES.decrypt(encrypted,key,
                                         {
                                         iv:iv,
                                         mode:CryptoJS.mode.CBC,
                                         padding:CryptoJS.pad.Pkcs7
                                         });
    return decrypted.toString(CryptoJS.enc.Utf8);
}
function getAES(){ //加密
    var data = "123456";//明文
    var key  = 'aaaa';  //密钥
    var iv   = 'bbbb';
    var encrypted = getAesString(data,key,iv); //密文
    var enstr = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);//encrypted.toString();
    //                     document.getElementById("encrypted").innerHTML = encrypted;
}
 
//                 function getDAes(){//解密
//                     var encrypted = document.getElementById("encrypted").innerHTML; //密文
//                     var key  = '1234567812345678';
//                     var iv   = 'Pkcs7';
//                     var decryptedStr = getDAesString(encrypted,key,iv);
//                     document.getElementById("decrypted").innerHTML = decryptedStr;
//                 };
getAES();

转载原文链接:https://blog.csdn.net/u010200636/article/details/53004754