Detailed explanation of AES encryption _javascript techniques in Nodejs and JavaScript

Source: Internet
Author: User
Tags base64 decrypt pkcs7

First, Introduction

1.aes encryption, in cryptography, also known as Rijndael encryption, is a block encryption standard used by the U.S. federal government. This standard, which replaces the original DES, has been analyzed and widely used worldwide. The Advanced Encryption Standard has become one of the most popular algorithms in symmetric key cryptography.

The 2.AES block length is fixed to 128 bits, and the key length can be 128,192 or 256 bits, while the key and block length used by Rijndael can be 32-bit integer multiples, with a 128-bit limit and 256-bit as the upper bound. including AES-ECB,AES-CBC,AES-CTR,AES-OFB,AES-CFB.

3. Here we only accept the usual ECB mode + pkcs7padding (same as the pkcs5padding value) to fill the encryption.

Second, the application

Use of AES in 1.nodejs

var crypto = require (' crypto ');

var Aesutil = Module.exports = {}; /** * AES Encryption * @param data to be encrypted content * @param key must be a 32-bit private key * @returns {string} */aesutil.encryption = function (data, key , iv) {IV = IV | |
 "";
 var clearencoding = ' UTF8 ';
 var cipherencoding = ' base64 ';
 var cipherchunks = [];
 var cipher = Crypto.createcipheriv (' AES-256-ECB ', key, iv);
 Cipher.setautopadding (TRUE);
 Cipherchunks.push (cipher.update (data, clearencoding, cipherencoding));
 Cipherchunks.push (Cipher.final (cipherencoding));
Return Cipherchunks.join ("); /** * AES Decryption * @param data to be decrypted content * @param key must be a 32-bit private key * @returns {string} */aesutil.decryption = function (data,
 Key, IV) {if (!data) {return ""; } IV = IV | |
 "";
 var clearencoding = ' UTF8 ';
 var cipherencoding = ' base64 ';
 var cipherchunks = [];
 var decipher = crypto.createdecipheriv (' AES-256-ECB ', key, iv);
 Decipher.setautopadding (TRUE);
 Cipherchunks.push (decipher.update (data, cipherencoding, clearencoding)); Cipherchunks.push (dEcipher.final (clearencoding));
Return Cipherchunks.join (");
 
 }

Use of AES in 2.javascript

Download Third Party library crypto-js.js git address: https://github.com/brix/crypto-js

To introduce the crypto-js.js under SRC, encrypt the code as follows:

var key = "12345678"  //Secret key must be: 8/16/32 bit
var message = "123456";

Encrypted
var encrypt = CryptoJS.AES.encrypt (message, CryptoJS.enc.Utf8.parse (key), {
 Mode:CryptoJS.mode.ECB,
 PADDING:CRYPTOJS.PAD.PKCS7
});
Console.log ("Value:" +encrypt);

Decrypt
var decrypt = CryptoJS.AES.decrypt (Encrypt, CryptoJS.enc.Utf8.parse (key), {
 Mode:CryptoJS.mode.ECB,
 PADDING:CRYPTOJS.PAD.PKCS7
});
Console.log ("Value:" +decrypt.tostring (CryptoJS.enc.Utf8));

The original author: Xian. Wang Lei
Original source: http://vipstone.cnblogs.com/

The above is the entire content of this article, I hope to help you learn.

Related Article

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.