const crypto = require('crypto');
// 生成随机的密钥和初始向量(IV)
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
// 要加密的数据
const text = 'Hello, World!';
// 创建密码器对象
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
// 加密数据
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
// 创建解密器对象
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
// 解密数据
const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
console.log('Original:', text);
console.log('Encrypted:', encrypted.toString('hex'));
console.log('Decrypted:', decrypted.toString());