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 !

更新於:2021年5月20日

2K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.