📅  最后修改于: 2023-12-03 15:13:16.965000             🧑  作者: Mango
AES (Advanced Encryption Standard) is a symmetric encryption algorithm used in cryptography. AES 256 is a variant of AES with a key size of 256 bits.
In this tutorial, we will be discussing how to implement AES 256 encryption and decryption in Node.js using the crypto module.
To follow along with the tutorial, you should have the following installed on your system:
npm init
command to create a new package.json file.mkdir aes-256
cd aes-256
npm init
crypto
module using npm install crypto
.npm install crypto
app.js
in the project directory and open it in your text editor.The following code shows how to encrypt a string using AES 256 algorithm.
const crypto = require('crypto');
const algorithm = 'aes-256-cbc'; // algorithm to be used
const key = crypto.randomBytes(32); // generate a random key
const iv = crypto.randomBytes(16); // generate a random initialization vector
const plaintext = 'This is my plaintext'; // plaintext to be encrypted
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(plaintext, 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(`Encrypted: ${encrypted}`); // the encrypted string
crypto
module.aes-256-cbc
. cbc
is Cipher Block Chaining, which is a mode of operation for block ciphers.createCipheriv
method, passing the algorithm
, key
, and iv
.update
method on the cipher object, passing the plaintext
, the encoding utf8
, and the desired output encoding, which is hex
.final
method on the cipher object, passing the desired output encoding.update
and final
methods.The following code shows how to decrypt the previously encrypted string using the same AES 256 algorithm.
const crypto = require('crypto');
const algorithm = 'aes-256-cbc'; // algorithm to be used
const key = crypto.randomBytes(32); // generate a random key
const iv = crypto.randomBytes(16); // generate a random initialization vector
const plaintext = 'This is my plaintext'; // plaintext to be encrypted
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(plaintext, 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(`Encrypted: ${encrypted}`); // the encrypted string
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
console.log(`Decrypted: ${decrypted}`); // the decrypted plaintext
crypto
module.algorithm
, key
, and iv
.update
method on the cipher object, passing the plaintext
, the encoding utf8
, and the desired output encoding, which is hex
.final
method on the cipher object, passing the desired output encoding. The encrypted
string is the result.algorithm
, key
, and iv
.update
method on the decipher object, passing the encrypted
string, the encoding hex
, and the desired output encoding, which is utf8
.final
method on the decipher object, passing the desired output encoding. The decrypted
plaintext is the result.AES 256 is a widely used encryption algorithm that provides a high level of security. In this tutorial, we have discussed how to implement AES 256 encryption and decryption in Node.js using the crypto module. You can use this knowledge to build secure communication systems or store sensitive data.