navsqi / aes-256-cbc-encryption

Encrypting and decrypting text using the AES-256-CBC (Advanced Encryption Standard with 256-bit key size in Cipher Block Chaining mode) algorithm

Repository from Github https://github.comnavsqi/aes-256-cbc-encryptionRepository from Github https://github.comnavsqi/aes-256-cbc-encryption

AES-256-CBC Encryption/Decryption Module

Code coverage License

This module provides functions to encrypt and decrypt text using AES-256-CBC in Node.js.

Installation

Ensure you have Node.js installed on your system. You can install the module by cloning the repository and installing the dependencies:

npm i aes-256-cbc-encryption

Usage

Encrypt Function

The encrypt function encrypts a given text using a 32-byte key and a 16-byte initialization vector (IV).

Parameters

  • text (string): The text to be encrypted.
  • key (string): A key with a length of 32 bytes (256 bits).
  • initVector (string): A 16-byte initialization vector (IV).

Returns

  • string: The encrypted text in base64 format.

Decrypt Function

The decrypt function decrypts a given encrypted text using a 32-byte key and a 16-byte initialization vector (IV).

Parameters

  • text (string): The encrypted text in base64 format.
  • key (string): A key with a length of 32 bytes (256 bits).
  • initVector (string): A 16-byte initialization vector (IV).

Returns

  • string: The decrypted text.

Example

Here's an example of how to use the module:

Set environment variables, for example

# .env
AES_KEY=bf3c199c2470cb477d907b1e0917c17b
AES_IV=5183666c72eec9e4
// index.js
const aes = require("aes-256-cbc-encryption"); // Replace with actual path to your module

const text = "Hello, World!";

// Encrypt the text
const encryptedText = aes.encrypt(text);
console.log(`Encrypted: ${encryptedText}`);

// Decrypt the text
const decryptedText = aes.decrypt(encryptedText);
console.log(`Decrypted: ${decryptedText}`);

OR you can passing parameter directly

// index.js
const aes = require("aes-256-cbc-encryption"); // Replace with actual path to your module

const text = "Hello, World!";
const key = "your-32-byte-long-key-goes-here-123456789012"; // Example key
const initVector = "your-16-byte-iv123"; // Example IV

// Encrypt the text
const encryptedText = aes.encrypt(text, key, initVector);
console.log(`Encrypted: ${encryptedText}`);

// Decrypt the text
const decryptedText = aes.decrypt(encryptedText, key, initVector);
console.log(`Decrypted: ${decryptedText}`);

Credits

Nauval S

License

MIT License

About

Encrypting and decrypting text using the AES-256-CBC (Advanced Encryption Standard with 256-bit key size in Cipher Block Chaining mode) algorithm

License:MIT License


Languages

Language:TypeScript 94.3%Language:JavaScript 5.7%