AES Encryption and Decryption in Javascript

How to use

  • Download aes.js

    Download

  • Javascript code

<script type="text/javascript">
    var data = '1234567890';
    var AES = {
        key: CryptoJS.enc.Utf8.parse('ASDFGHJKLZXCVBNM'),
        iv: CryptoJS.enc.Utf8.parse('ASDFGHJKLZXCVBNM'),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
        /*encryption*/
        encrypt: function(data) {
            data = CryptoJS.AES.encrypt(data, this.key, {
                iv: this.iv,
                mode: this.mode,
                padding: this.padding
            });
            return data.ciphertext.toString(CryptoJS.enc.Base64);
        },
        /*decryption*/
        decrypt: function(data) {
            data = CryptoJS.AES.decrypt(data, this.key, {
                iv: this.iv,
                mode: this.mode,
                padding: this.padding
            });
            return data.toString(CryptoJS.enc.Utf8);
        }
    };
    //encryption
    var endata = AES.encrypt(data);
    //descyption
    var dedata = AES.decrypt(endata);
    console.log('AES endata:', endata);
    console.log('AES dedata:', dedata);
</script>
  • Testing result

   

你可能感兴趣的:(JavaScript,aes,encryption,CryptoJS,Decryption)