node.js内置的crypto加解密库

下载库

npm install crypto --save

如何在Node.js中加密数据

为了开始,创建app.js 文件,并定义我们的加密函数,如下所示。

首先,你将导入crypto 模块。

const crypto = require ("crypto");

在加密数据的同时,使用一种算法是至关重要的。在这个项目中,我们使用aes-256-cbc

crypto.randomBytes() 方法被用来生成在编写的代码中产生的密码学构建的随机数据。

这里使用initVector (初始化向量)来保存来自randomBytes() 方法的16字节的随机数据,Securitykey 包含32字节的随机数据。

// crypto module
const crypto = require("crypto");

const algorithm = "aes-256-cbc"; 

// generate 16 bytes of random data
const initVector = crypto.randomBytes(16);

// protected data
const message = "This is a secret message";

// secret key generate 32 bytes of random data
const Securitykey = crypto.randomBytes(32);

为了对数据进行加密,使用了cipher 函数。我们项目的cipher 函数是使用createCipheriv() ,来自crypto 模块的初始化向量制作的。

将第一个参数作为我们使用的算法,第二个参数为Securitykey ,第三个参数为initVector

为了加密信息,在cipher 上使用update() 方法。将第一个参数作为message ,第二个参数作为utf-8 (输入编码),第三个参数作为hex (输出编码)。

// crypto module
const crypto = require("crypto");

const algorithm = "aes-256-cbc"; 

// generate 16 bytes of random data
const initVector = crypto.randomBytes(16);

// protected data
const message = "This is a secret message";

// secret key generate 32 bytes of random data
const Securitykey = crypto.randomBytes(32);

// the cipher function
const cipher = crypto.createCipheriv(algorithm, Securitykey, initVector);

// encrypt the message
// input encoding
// output encoding
let encryptedData = cipher.update(message, "utf-8", "hex");

该代码告诉cipher ,使用final() 方法停止加密。当final() 方法被调用时,cipher 不能再被用来加密数据。

然后,信息就被加密了,恶意攻击者就无法理解加密后的数据。下面是一个关于如何加密数据的例子。

// crypto module
const crypto = require("crypto");

const algorithm = "aes-256-cbc"; 

// generate 16 bytes of random data
const initVector = crypto.randomBytes(16);

// protected data
const message = "This is a secret message";

// secret key generate 32 bytes of random data
const Securitykey = crypto.randomBytes(32);

// the cipher function
const cipher = crypto.createCipheriv(algorithm, Securitykey, initVector);

// encrypt the message
// input encoding
// output encoding
let encryptedData = cipher.update(message, "utf-8", "hex");

encryptedData += cipher.final("hex");

console.log("Encrypted message: " + encryptedData);

下面是输出结果。

如何在Node.js中解密数据

解密数据的格式与加密数据的格式类似。在我们的Node.js项目中,我们将使用decipher 函数来解密数据。因此,我们的项目对数据进行加密和解密。

下面是一个如何解密数据的例子。

// the decipher function
const decipher = crypto.createDecipheriv(algorithm, Securitykey, initVector);

let decryptedData = decipher.update(encryptedData, "hex", "utf-8");

decryptedData += decipher.final("utf8");

console.log("Decrypted message: " + decryptedData);

按照下面的例子,使用crypto对数据进行加密和解密。

// crypto module
const crypto = require("crypto");

const algorithm = "aes-256-cbc"; 

// generate 16 bytes of random data
const initVector = crypto.randomBytes(16);

// protected data
const message = "This is a secret message";

// secret key generate 32 bytes of random data
const Securitykey = crypto.randomBytes(32);

// the cipher function
const cipher = crypto.createCipheriv(algorithm, Securitykey, initVector);

// encrypt the message
// input encoding
// output encoding
let encryptedData = cipher.update(message, "utf-8", "hex");

encryptedData += cipher.final("hex");

console.log("Encrypted message: " + encryptedData);

// the decipher function
const decipher = crypto.createDecipheriv(algorithm, Securitykey, initVector);

let decryptedData = decipher.update(encryptedData, "hex", "utf-8");

decryptedData += decipher.final("utf8");

console.log("Decrypted message: " + decryptedData);

下面是输出结果。

这里是引用

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值