• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

node.js内置的crypto加解密库

武飞扬头像
Zyx_Lz
帮助1

下载库

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);
学新通

下面是输出结果。

这里是引用

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbeiif
系列文章
更多 icon
同类精品
更多 icon
继续加载