crypto.createDecipheriv()方法是加密模块的内置应用程序编程接口,用于创建带有所述算法, key 和初始化向量(即(iv))的解密对象。
用法:
crypto.createDecipheriv( algorithm, key, iv, options )
参数:此方法接受上述和以下所述的四个参数:
- algorithm:它是一个字符串类型值,取决于OpenSSL。示例是aes192,aes256等。
- key:这是算法和iv使用的原始 key 。它包含字符串,Buffer,TypedArray或DataView。 key 可以是可选的secret类型的KeyObject。
- iv:它是一个初始化向量,必须是不确定的并且非常独特。但是,理想的iv在密码上是随机的。不必保密。它可以保存字符串,Buffer,TypedArray或DataView类型的数据。如果密码不需要iv,则可以为null。
- options:它是一个可选参数,用于控制流的行为。除非在CCM或OCB模式下使用密码(例如“ aes-128-ccm”),否则它是可选的。在这种情况下,需要authTagLength选项,该选项定义了身份验证标签的长度(字节),而在GCM模式下,不需要authTagLength选项,但可以使用它来设置身份验证标签的长度,该长度将由getAuthTag()方法,默认值为16个字节。
返回值:它返回解密对象。
以下示例说明了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
相关用法
- Node.js GM modulate()用法及代码示例
- Node.js GM monochrome()用法及代码示例
- Node.js GM equalize()用法及代码示例
- Node.js GM enhance()用法及代码示例
- Node.js GM threshold()用法及代码示例
- Node.js GM thumbnail()用法及代码示例
- Node.js GM whitePoint()用法及代码示例
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | crypto.createDecipheriv() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。