JS and JAVA implementation AES (aes/cbc/pkcs5padding) encryption __java

Source: Internet
Author: User
Tags decrypt pkcs7

Introduction to AES



The Advanced Encryption Standard in cryptography (Advanced encryption Standard,aes), also known as the Advanced Encryption Standard Rijndael encryption method,
is a block encryption standard used by the U.S. federal government. This standard replaces the original DES, has been analyzed by many parties and is widely used worldwide
are used. After five years of selection process, advanced encryption standards by the United States National Institute of Standards and Technology (NIST) on November 26, 2001
Posted in FIPS PUB197 and became a valid standard on May 26, 2002. 2006, the Advanced Encryption Standard has become symmetric key encryption
One of the most popular algorithms. The algorithm is designed by the Belgian cipher scientists Joan Daemen and Vincentrijmen, combining the names of two authors
Words, named after Rijndael, submit the selection process of Advanced Encryption Standard. (Rijdael pronunciation is nearly "rhinedoll".) 


AES Encryption mode and fill mode (in fact, there are several filling methods are not written, at the beginning also around here)

Algorithm/mode/padding —————-16-byte encrypted data length--less than 16 bytes after encryption length
Aes/cbc/nopadding ——-——————————-not supported
Aes/cbc/pkcs5padding ——-——————————-16
Aes/cbc/iso10126padding ——-——————————-16
Aes/cfb/nopadding ——-——————————-Raw data length
Aes/cfb/pkcs5padding ——-——————————-16
Aes/cfb/iso10126padding ——-——————————-16
Aes/ecb/nopadding ——-——————————-not supported
Aes/ecb/pkcs5padding ——-——————————-16
Aes/ecb/iso10126padding ——-——————————-16
Aes/ofb/nopadding ——-——————————-Raw data length
Aes/ofb/pkcs5padding ——-——————————-16
Aes/ofb/iso10126padding ——-——————————-16
Aes/pcbc/nopadding ——-——————————-not supported
Aes/pcbc/pkcs5padding ——-——————————-16
Aes/pcbc/iso10126padding ——-——————————-16



More about encryption mode content: http://blog.sina.com.cn/s/blog_679daa6b0100zmpp.html JavaScript-side code:




<script src= "Aes.js" ></script>
<script src= "Pad-zeropadding.js" ></script>
< Script>
var data = "Test String";
var key  = CryptoJS.enc.Latin1.parse (' 1234567812345678 ');
var IV   = CryptoJS.enc.Latin1.parse (' 1234567812345678 ');

Encrypted
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/> ');


Decrypt
var decrypted = CryptoJS.AES.decrypt (encrypted,key,{iv:iv,padding:cryptojs.pad.zeropadding});
Console.log (decrypted.tostring (CryptoJS.enc.Utf8));
</script>


The second type:


AES-128-CBC encryption mode, key needs to be 16 bits, key and IV can be the same
    function encrypt (data) {
        var key  = CryptoJS.enc.Latin1.parse (' Dufy20170329java ');
        var IV   = CryptoJS.enc.Latin1.parse (' Dufy20170329java ');
        return CryptoJS.AES.encrypt (data, key, {iv:iv,mode:cryptojs.mode.cbc,padding:cryptojs.pad.zeropadding}). toString ( );
    }


Use CRYPTOJS to complete encryption in Vue

The introduction of AES encryption
import CRYPTOJS from ' Crypto-js '
vue.prototype. $crypto = Cryptojs;

Encrypt  CryptoJS.pad.Pkcs7
var key  = this. $crypto. Enc.Utf8.parse (' xxxxx ');  Unified with the background
var IV   = this. $crypto. Enc.Utf8.parse (' 16-bytes--string ');//Background Unified
var ciphertext = this.$ Crypto. Aes.encrypt (data, key,{
    iv:iv,
    mode:this. $crypto MODE.CBC,//modified according to the corresponding pattern above
    padding:this.$ CRYPTO.PAD.PKCS7//modified according to the above corresponding mode
}). toString ();


Aes,cryptojs Download


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.