nodejs如何利用3DES加密解密

2024年09月20日 建站教程

nodejs如何利用3DES进行加密解密,下面web建站小编给大家简单介绍一下!

安装脚手架

npm install crypto

生成密钥

const crypto = require('crypto');
 
const key = crypto.randomBytes(24);
console.log(key.toString('hex'));

加密数据

const crypto = require('crypto');

const key = crypto.randomBytes(24);
const text = 'Hello, world!';
 
const cipher = crypto.createCipheriv('des-ede3', key, '');
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(encrypted);

解密数据

const crypto = require('crypto');
 
const key = crypto.randomBytes(24);
const text = 'Hello, world!';
 
const cipher = crypto.createCipheriv('des-ede3', key, '');
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(encrypted);
 
const decipher = crypto.createDecipheriv('des-ede3', key, '');
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
console.log(decrypted);

本文链接:http://so.lmcjl.com/news/13464/

展开阅读全文
相关内容