使用Crypto在Node.js中进行数据加密和解密

6,096 阅读7分钟

在靠近用户的地方部署容器

本工程教育(EngEd)计划由科支持。

在全球范围内即时部署容器。Section是经济实惠、简单而强大的。

免费开始

使用Crypto在Node.js中进行数据加密和解密

6月23日, 2021

网络应用程序可以访问属于人们、组织和政府的大量数据。数据被访问得越多,对数据安全的威胁就越大。在软件开发行业,开发人员使用密码学和加密技术来保护敏感数据不受恶意攻击。

密码学被用来保护存储在数据库中或通过软件开发行业网络传输的数据。在处理、移动和存储数据时,你必须做到安全和可靠。

因此,作为一个node.js开发人员,你应该了解如何加密和解密数据,以确保系统处理的数据安全。Node.js有一个内置库,称为crypto ,用于数据加密和解密。

加密和解密的目的是增强安全性。本文将帮助你学习如何使用Node.js的crypto 模块来加密和解密你的应用程序中的数据。此外,它还将总结node.js中的密码学

目录

前提条件

在阅读本文之前,需要对密码学和node.js有一个全面的了解。此外,你应该有。

node.js中的密码学

密码学对软件开发至关重要。数据必须得到保护。密码学是关于如何保持数据安全的技术研究。它通过将明文转换为不可读的文本,反之亦然,将数据转换为秘密。因此,只有该数据的发送者和接收者能够理解其内容。

加密系统的三个主要组成部分包括明文、密文和算法。为了使信息成为秘密,我们使用一个密码和一个算法,将明文变成密码文本。将数据转换成不可读的文本被称为加密,而将其逆转为明文则是解密。

加密算法使用一个密钥将明文转换为密文。只有当你带着正确的密钥时,才有可能将密码文本转换回明文。

如果你使用相同的密钥对数据进行加密和解密,你就使用对称加密。如果使用不同的密钥进行加密和解密,则使用非对称加密

为了保护Node.js应用程序中的数据,你必须在数据库中存储散列的密码。这样,你就不能在数据被散列后将其转换成明文。它必须被验证。

如果恶意攻击者获得了数据库的访问权,他们将不会读取数据,因为它是加密的。此外,他们没有钥匙来帮助他们这样做。

Node.js加密模块

Node.jscrypto 模块提供了加密操作,帮助你保护你的Node.js应用程序。它支持哈希值、用于验证的HMAC、密码器、解密器等。

如前所述,crypto 是Node.js的一个内置库。因此,在你的Node.js应用程序中使用它之前,它不需要安装和配置。crypto 模块处理一种执行数据加密和解密的算法。

crypto 模块授权你在将数据存储到数据库之前对纯文本进行散列。散列数据不能像加密数据那样用特定的密钥解密。相反,HMAC负责基于哈希的消息验证码,它对密钥和值进行哈希处理,以创建一个最终的哈希。

你可能想为传输目的对数据进行加密和解密。这就是cipherdecipher 函数的用处。你用cipher 对数据进行加密,用decipher 对数据进行解密。另外,你可能想在将数据存储到数据库之前对其进行加密。

为了验证加密的或散列的密码。最好是有一个verify 功能。让我们探索数据加密和解密,并使用crypto 来实现Node.js应用程序。

开始使用Node.js项目

我们将创建一个Node.js项目来使用crypto 。你将学习如何加密和解密数据。要开始,请执行这个命令。

npm init -y

默认情况下,crypto 模块是一个内建的Node.js库。但如果Node.js是手动安装的,crypto 可能不会与之一起交付。要安装,请执行以下命令。

npm install crypto --save

如果crypto ,你不需要执行该命令,因为它是使用预建包安装的。

如何在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);

下面是输出结果。

Data encryption output

如何在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);

下面是输出结果。

Data encryption and decryption output

总结

这篇文章探讨了在Node.js中使用crypto 模块进行数据加密和解密。此外,它还涉及到了。

  • Node.js中的密码学。
  • Node.js crypto模块。

我希望你已经获得了关于加密和解密以及如何在Node.js应用程序中使用crypto 模块来实现加密和解密的坚实知识。


同行评审贡献者:。Mohan Raj

类似文章

[

Building a payroll system with next.js Hero Image

语言, Node.js

用Next.js构建一个薪资系统

阅读更多

](www.section.io/engineering…

Feed-forward and Recurrent Neural Networks Python Implementation Hero Image

语言, Node.js

在React Native中使用React Navigation和嵌套Navigators进行路由选择

阅读更多

](www.section.io/engineering…

Ethers vs web3 Hero Image

区块链, Node.js

Ethers vs. Web3

阅读更多

](www.section.io/engineering…)