在Node.js中使用AES-256-CBC加密算法来加密和解密数据

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());

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容