📜  aes 256 nodejs - Javascript(1)

📅  最后修改于: 2023-12-03 15:13:16.965000             🧑  作者: Mango

AES 256 Node.js - Javascript
Introduction

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.

Prerequisites

To follow along with the tutorial, you should have the following installed on your system:

  • Node.js
  • Npm
  • A text editor (such as Visual Studio Code or Sublime Text)
Setting up the project
  1. Create a new directory for the project and run npm init command to create a new package.json file.
mkdir aes-256
cd aes-256
npm init
  1. Install the crypto module using npm install crypto.
npm install crypto
  1. Create a new file named app.js in the project directory and open it in your text editor.
AES Encryption

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

Explanation

  1. Import the crypto module.
  2. Define the encryption algorithm to be used, which is aes-256-cbc. cbc is Cipher Block Chaining, which is a mode of operation for block ciphers.
  3. Generate a random key with a length of 32 bytes (256 bits).
  4. Generate a random initialization vector (IV) with a length of 16 bytes.
  5. Define the plaintext that will be encrypted.
  6. Create a new cipher object using the createCipheriv method, passing the algorithm, key, and iv.
  7. Call the update method on the cipher object, passing the plaintext, the encoding utf8, and the desired output encoding, which is hex.
  8. Call the final method on the cipher object, passing the desired output encoding.
  9. The encrypted string is the result of calling update and final methods.
AES Decryption

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

Explanation

  1. Import the crypto module.
  2. Define the encryption algorithm to be used.
  3. Generate a random key.
  4. Generate a random initialization vector (IV).
  5. Define the plaintext that will be encrypted.
  6. Create a new cipher object using the same algorithm, key, and iv.
  7. Call the update method on the cipher object, passing the plaintext, the encoding utf8, and the desired output encoding, which is hex.
  8. Call the final method on the cipher object, passing the desired output encoding. The encrypted string is the result.
  9. Create a new decipher object using the same algorithm, key, and iv.
  10. Call the update method on the decipher object, passing the encrypted string, the encoding hex, and the desired output encoding, which is utf8.
  11. Call the final method on the decipher object, passing the desired output encoding. The decrypted plaintext is the result.
Conclusion

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.