1. Introduction
1. Aes encryption, in cryptography, is also known as Rijndael encryption method, which is a block encryption standard adopted by the US federal government. This standard is used to replace the original DES, and has been analyzed by multiple parties and is widely used worldwide. Advanced encryption standards have become one of the most popular algorithms in symmetric key encryption.
2. The block length of AES 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 an integer multiple of 32 bits, with 128 bits as the lower limit and 256 bits as the upper limit. Including AES-ECB, AES-CBC, AES-CTR, AES-OFB, AES-CFB.
3. Here we only accept the commonly used ECB method + pkcs7padding (the same value as pkcs5padding) to fill encryption.
2. Application
1.Using aes in nodejs
var crypto = require('crypto');var aesutil = module.exports = {};/** * aes encryption* @param data Content to be encrypted* @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 Content to be decrypted* @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('');}2.Using aes in javascript
Download the third-party library Crypto-js.js git address: https://github.com/brix/crypto-js
Introduce crypto-js.js under src, the encryption code is as follows:
var key = "12345678" //The key must be: 8/16/32-bit var message = "123456";//Encrypt 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));Original author: Xi'an Wang Lei
Original source: http://vipstone.cnblogs.com/
The above is all about this article, I hope it will be helpful to everyone's learning.