aes 256 nodejs

const crypto = require('crypto');

// Step 1: Generate a random 256-bit (32-byte) key
const key = crypto.randomBytes(32);

// Step 2: Create an AES-256-CBC cipher object with the generated key
const cipher = crypto.createCipher('aes-256-cbc', key);

// Step 3: Provide the data to be encrypted and specify the input encoding (e.g., 'utf-8')
const inputData = 'Hello, World!';
const encryptedData = cipher.update(inputData, 'utf-8', 'hex') + cipher.final('hex');

// Step 4: Output the encrypted data
console.log('Encrypted Data:', encryptedData);

// Step 5: Create an AES-256-CBC decipher object with the same key
const decipher = crypto.createDecipher('aes-256-cbc', key);

// Step 6: Provide the encrypted data and specify the output encoding
const decryptedData = decipher.update(encryptedData, 'hex', 'utf-8') + decipher.final('utf-8');

// Step 7: Output the decrypted data
console.log('Decrypted Data:', decryptedData);