Javascript下aes加解密

2019PHP高薪工程师学习路线图....>>>

需要在https://code.google.com/p/crypto-js/下载工具包

<script type="text/javascript" src="aes.js"></script>
    <script type="text/javascript" src="pad-zeropadding.js"></script>

导入文件,aes.js需要导入crypto-js压缩包中rollups文件夹下的那个aes.js文件,如果引入的是components文件夹下的aes.js是会报错的
  <script type="text/javascript">
        var data = "Test String";
        var key  = CryptoJS.enc.Latin1.parse('1234567812345678');
        var iv   = CryptoJS.enc.Latin1.parse('1234567812345678');

        //加密
        var encrypted = CryptoJS.AES.encrypt(data,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});

        document.write(encrypted.ciphertext);
        document.write('<br/>');
        document.write(encrypted.key);
        document.write('<br/>');
        document.write(encrypted.iv);
        document.write('<br/>');
        document.write(encrypted.salt);
        document.write('<br/>');
        document.write(encrypted);
        document.write('<br/>');

        //解密
        var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding});
        console.log(decrypted.toString(CryptoJS.enc.Utf8));
    </script>

成功加解密,最后解密的字符串串在浏览器的控制台里才能看到。