Cryptojs AES256 (CBC) encryption algorithm simple use of __ algorithm

Source: Internet
Author: User
1.CryptoJS Script Download

Download Address: Http://pan.baidu.com/s/1slwHVLb

2.WordArray (an array of 32-bit words.)

Before using AES encryption, understand the next wordarray, I understand it as the new data type defined in Cryptojs, called "Word array".

2.1: Initializing

var Wordarray = CryptoJS.lib.WordArray.create ();//Create an empty Wordarray object

2.2:wordarray object->16 into string

var string = wordarray.tostring ();//default CryptoJS.enc.Hex, that is, the 16 binary string
 var string = wordarray.tostring ( CryptoJS.enc.Utf8);//utf-8 string

2.3:16 binary string->wordarray object

var Wordarray = CryptoJS.enc.Hex.parse (hexstring);

2.4:wordarray Object->utf8 string

var utf8string = CryptoJS.enc.Utf8.stringify (Wordarray);
Equivalent to 2.2 wordarray.tostring (CRYPTOJS.ENC.UTF8);

2.5:utf8 string->wordarray object

var Wordarray = CryptoJS.enc.Utf8.parse (utf8string);

2.6:wordarray Object->base64 string

var base64string = CryptoJS.enc.Base64.stringify (Wordarray);

2.7:base64 string->wordarray object

var Wordarray = CryptoJS.enc.Base64.parse (base64string);
3. Simple use
<script src= "<%=request.getcontextpath ()%>/js/cryptojs/crypto-js.js" ></script>
<script> var message = "1_2_3_4_5_6_7_8_9_0";//utf8 string, to be encrypted var IV = CryptoJS.lib.WordArray.random (128/8). toString (CryptoJS.enc.Hex);//randomly generate a 16 binary string of length 32.

IV is called the initial vector, and the different IV encrypted strings are different, and encryption and decryption require the same IV. var key = "0321ebeba1f75de2d3cd3471af7418a4";//Secret key.
The 16 string of length 32. 
var cryptkey = CryptoJS.enc.Hex.parse (key)//convert 16 string to Wordarray object////var key = "QWERTYUIOPASDFGH";//UTF8 string of length 16

var cryptkey = CryptoJS.enc.Utf8.parse (key)//convert UTF8 string to Wordarray object//The key is to convert to a Wordarray object, which is used when encrypting. Test var ciphertext = Aesencrypt (MESSAGE,CRYPTKEY,IV);//encrypt var decryptedmessage = Aesdecrypt (CIPHERTEXT,CRYPTKEY,IV); /decryption alert (decryptedmessage);//1_2_3_4_5_6_7_8_9_0 </script> 
<script>
//** Encryption * *
//var ciphertext = CryptoJS.AES.encrypt (message, key, CFG);
Params: Note that the parameter key is Wordarray object
//return: Password object or Password object Base64 string
function Aesencrypt (message,key,iv) {
    var Ciphertext = CryptoJS.AES.encrypt (message, key, {   
        Iv:CryptoJS.enc.Hex.parse (iv),
        Mode:CryptoJS.mode.CBC,
        PADDING:CRYPTOJS.PAD.PKCS7 
    });
    return ciphertext;//Password object (obejct type, non-wordarray type), BASE64 encoded.
    //return ciphertext.tostring ();//Password Object Base64 string

}

* * Decryption * * *
//var plaintext  = CryptoJS.AES.decrypt (ciphertext, key, CFG);
Params: Note that the parameter ciphertext must be a Base64 encoded object or string.
function Aesdecrypt (ciphertext,key,iv) {
    var decrypted = CryptoJS.AES.decrypt (ciphertext,key,{ 
        IV: CryptoJS.enc.Hex.parse (iv),
        Mode:CryptoJS.mode.CBC,
        padding:CryptoJS.pad.Pkcs7 
    });
    Return decrypted.tostring (CryptoJS.enc.Utf8);//wordarray object to UTF8 string
}

</script>
4. Extended

The AES cryptographic function return value is the cryptographic object ciphertext, whose properties:
* Ciphertext:ciphertextwordarray,
* Key:keywordarray,
* Iv:ivwordarray,
* Algorithm:CryptoJS.algo.AES,
* Mode:CryptoJS.mode.CBC,
* Padding:CryptoJS.pad.PKCS7,
* Blocksize:4,
* Formatter:CryptoJS.format.OpenSSL

Property ciphertext, which is the ciphertext, can be found to be a wordarray type. By encrypting the object. Property name value.

function Aesencrypt (message,key,iv) {
    var ciphertext = CryptoJS.AES.encrypt (message, key, {   
        IV: CryptoJS.enc.Hex.parse (iv),
        Mode:CryptoJS.mode.CBC,
        padding:CryptoJS.pad.Pkcs7 
    });
    Return CryptoJS.enc.Base64.stringify (Ciphertext.ciphertext);//ciphertext Turn Base64 string
}

//decryption function slightly changed.
function Aesdecrypt (crypted,key,iv) {
    var decrypted = CryptoJS.AES.decrypt ({ciphertext: CryptoJS.enc.Base64.parse (crypted)},key,{ 
        Iv:CryptoJS.enc.Hex.parse (iv),
        Mode:CryptoJS.mode.CBC,
        PADDING:CRYPTOJS.PAD.PKCS7 
    });
    Return decrypted.tostring (CryptoJS.enc.Utf8);
}
4. Summary

Many of them are their own understanding, I hope there will be no big deviation. Recently this project, has encountered the AES encryption, only then has the understanding. It is a long way to repair the long ah ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.