Node.js 中的 crypto.createDecipheriv() 方法
crypto.createCipheriv() 是来自 'crypto' 模块的编程接口。它将根据给定的算法、密钥、iv 和函数中传递的选项创建并返回 Decipher 对象。
语法
crypto.createDecipheriv(algorithm, key, iv, [options])
参数
上述参数描述如下:
algorithm – 它接收用于创建密码的算法的输入。一些可能的值是:aes192、aes256 等。
key – 它接收算法和 iv 使用的原始密钥的输入。可能的值类型可以是:字符串、缓冲区、TypedArray 或 DataView。它可以选择是 secret 类型的对象。
iv – 也称为初始化向量。此参数接收 iv 的输入,这将使密码不确定且唯一。它不需要是秘密的。其可能的值类型是:字符串、缓冲区、TypedArray、DataView。如果密码不需要,则可以为 null。
options – 这是一个可选参数,用于控制流行为。当密码在 CCM 或 OCB 模式下使用时(例如 'aes-256-ccm'),此参数不是可选的。
示例
创建一个名为 createDecipheriv.js 的文件并复制下面的代码片段。创建文件后,使用以下命令运行此代码,如下例所示:
node createDecipheriv.js
createDecipheriv.js
// A node demo program for creating the ECDH
// Importing the crypto module
const crypto = require('crypto');
// Initializing the algorithm
const algorithm = 'aes-192-cbc';
// Defining and initializing the password
const password = '123456789'
// Initializing the key
const key = crypto.scryptSync(password, 'TutorialsPoint', 24);
// Initializing the iv vector
const iv = Buffer.alloc(16, 0);
// Creating the Decipher with the above defined parameters
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = '';
// Reading and encrypting the data
decipher.on('readable', () => {
let chunk;
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString('utf8');
}
});
//Handling the closing/end event
decipher.on('end', () => {
console.log(decrypted);
});
// Encrypted data which is going to be decrypted
const encrypted = 'uqeQEkXy5dpJjQv+JDvMHw==';
// Printing the decrypted text
decipher.write(encrypted, 'base64');
decipher.end();
console.log("Completed... !");输出
C:\home
ode>> node createDecipheriv.js Completed... ! TutorialsPoint
示例
让我们再看一个例子。
// A node demo program for creating the ECDH
// Importing the crypto module
const crypto = require('crypto');
// Initializing the algorithm
const algorithm = 'aes-256-cbc';
// Defining and initializing the password
const password = '123456789'
// Initializing the key
const key = crypto.randomBytes(32);
// Initializing the iv vector
const iv = crypto.randomBytes(16);
// Encrypt function to encrypt the data
function encrypt(text) {
// Creating the cipher with the above defined parameters
let cipher =
crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
// Updating the encrypted text...
let encrypted = cipher.update(text);
// Using concatenation
encrypted = Buffer.concat([encrypted, cipher.final()]);
// Returning the iv vector along with the encrypted data
return { iv: iv.toString('hex'),
encryptedData: encrypted.toString('hex') };
}
//Decrypt function for decrypting the data
function decrypt(text) {
let iv = Buffer.from(text.iv, 'hex');
let encryptedText =
Buffer.from(text.encryptedData, 'hex');
// Creating the decipher from algo, key and iv
let decipher = crypto.createDecipheriv(
'aes-256-cbc', Buffer.from(key), iv);
// Updating decrypted text
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
// returning response data after decryption
return decrypted.toString();
}
// Encrypting the below data and printing output
var output = encrypt("Welcome to TutorialsPoint !");
console.log("Encrypted data -- ", output);
//Printing decrypted data
console.log("Decrypted data -- ", decrypt(output));输出
C:\home
ode>> node createDecipheriv.js Encrypted data -- { iv: '3fb2c84290e04d9bfb099bc65a7ac941', encryptedData: '4490777e90c5a78037cb92a99d561ae250562e2636af459b911cfa01c0191e3f' } Decrypted data -- Welcome to TutorialsPoint !
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP