Node.js crypto.createDecipheriv() 方法
crypto.createDecipheriv() 方法是加密模块的内置应用程序编程接口,用于创建Decipher对象,具有规定的算法、密钥和初始化向量,即 (iv)。
句法:
crypto.createDecipheriv( algorithm, key, iv, options )
参数:此方法接受上面提到的四个参数,如下所述:
- algorithm:它是一个依赖于OpenSSL的字符串类型值。例如aes192 、 aes256等。
- 密钥:它是算法使用的原始密钥,iv。它包含字符串、 Buffer 、 TypedArray或DataView 。密钥可以是可选的密钥类型密钥对象。
- iv:它是一个初始化向量,必须是不确定的并且非常独特。然而,理想的 iv 将是加密随机的。它不需要保密。它可以保存字符串 、 Buffer 、 TypedArray或DataView类型的数据。如果 cipher 不需要iv那么它可以是null 。
- options:它是一个可选参数,用于控制流的行为。它是可选的,除非在 CCM 或 OCB 模式下使用密码(例如“aes-128-ccm”)。在这种情况下,需要authTagLength选项来定义身份验证标签的长度(字节),而在GCM模式下,不需要 authTagLength选项,但它可用于设置将由以下方式返回的身份验证标签的长度getAuthTag() 方法,默认值为 16 个字节。
返回值:返回Decipher对象。
下面的示例说明了在 Node.js 中使用crypto.createDecipheriv() 方法:
示例 1:
// Node.js program to demonstrate the
// crypto.createDecipheriv() method
// Includes crypto module
const crypto = require('crypto');
// Defining algorithm
const algorithm = 'aes-192-cbc';
// Defining password
const password = 'bncaskdbvasbvlaslslasfhj';
// Defining key
const key = crypto.scryptSync(password, 'GfG', 24);
// Defininf iv
const iv = Buffer.alloc(16, 0);
// Creating decipher
const decipher =
crypto.createDecipheriv(algorithm, key, iv);
// Declaring decrypted
let decrypted = '';
// Reading data
decipher.on('readable', () => {
let chunk;
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString('utf8');
}
});
// Handling end event
decipher.on('end', () => {
console.log(decrypted);
});
// Encrypted data which is to be decrypted
const encrypted =
'MfHwhG/WPv+TIbG/qM78qA==';
decipher.write(encrypted, 'base64');
decipher.end();
console.log("done");
输出:
done
CS-Portal
示例 2:
// Node.js program to demonstrate the
// crypto.createDecipheriv() method
// Includes crypto module
const crypto = require('crypto');
// Difining algorithm
const algorithm = 'aes-256-cbc';
// Defining key
const key = crypto.randomBytes(32);
// Defining iv
const iv = crypto.randomBytes(16);
// An encrypt function
function encrypt(text) {
// Creating Cipheriv with its parameter
let cipher =
crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
// Updating text
let encrypted = cipher.update(text);
// Using concatenation
encrypted = Buffer.concat([encrypted, cipher.final()]);
// Returning iv and encrypted data
return { iv: iv.toString('hex'),
encryptedData: encrypted.toString('hex') };
}
// A decrypt function
function decrypt(text) {
let iv = Buffer.from(text.iv, 'hex');
let encryptedText =
Buffer.from(text.encryptedData, 'hex');
// Creating Decipher
let decipher = crypto.createDecipheriv(
'aes-256-cbc', Buffer.from(key), iv);
// Updating encrypted text
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
// returns data after decryption
return decrypted.toString();
}
// Encrypts output
var output = encrypt("GeeksforGeeks");
console.log(output);
// Decrypts output
console.log(decrypt(output));
输出:
{ iv: '6bbc47a2756d6d6bf315cfd3cc0b711a', encryptedData: 'fae9a6fb31c0b0668da8c3be1b1da81a' }
GeeksforGeeks
参考: https://nodejs.org/api/crypto.html#crypto_crypto_createdecipheriv_algorithm_key_iv_options