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);
下面是输出结果。
这里是引用
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgbeiif
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01