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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。