📜  Node.js crypto.createCipheriv() 方法

📅  最后修改于: 2022-05-13 01:56:49.202000             🧑  作者: Mango

Node.js crypto.createCipheriv() 方法

crypto.createCipheriv() 方法是 crypto 模块的内置应用程序编程接口,用于创建 Cipher 对象,具有规定的算法、密钥和初始化向量 (iv)。
句法:

crypto.createCipheriv( algorithm, key, iv, options )

参数:此方法接受上面提到的四个参数,如下所述:

  • algorithm:它是一个依赖于OpenSSL的字符串类型值。例如aes192aes256等。
  • 密钥:它是算法使用的原始密钥,iv。它包含字符串BufferTypedArrayDataView 。密钥可以是可选的密钥类型密钥对象。
  • iv:它是一个初始化向量,必须是不确定的并且非常独特。然而,理想的 iv 将是加密随机的。它不需要保密。它可以保存字符串BufferTypedArrayDataView类型的数据。如果 cipher 不需要iv那么它可以是null
  • options:它是一个可选参数,用于控制流的行为。它是可选的,除非在 CCM 或 OCB 模式下使用密码(例如“aes-128-ccm”)。在这种情况下,需要authTagLength选项来定义身份验证标签的长度(字节),而在GCM模式下,不需要 authTagLength选项,但它可用于设置将由以下方式返回的身份验证标签的长度getAuthTag() 方法,默认值为 16 个字节。

返回值:返回Cipher对象。
下面的示例说明了在 Node.js 中使用crypto.createCipheriv() 方法
示例 1:

javascript
// Node.js program to demonstrate the    
// crypto.createCipheriv() method
 
// Includes crypto module
const crypto = require('crypto');
 
// Defining 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') };
}
 
// Displays output
var output = encrypt("GeeksforGeeks");
console.log(output);


javascript
// Node.js program to demonstrate the    
// crypto.createCipheriv() 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 cipher
const cipher = crypto.createCipheriv(algorithm, key, iv);
 
// Declaring encrypted
let encrypted = '';
 
// Reading data
cipher.on('readable', () => {
  let chunk;
  while (null !== (chunk = cipher.read())) {
    encrypted += chunk.toString('base64');
  }
});
 
// Handling end event
cipher.on('end', () => {
console.log(encrypted);
});
 
// Writing data
cipher.write('CS-Portal');
cipher.end();
console.log("done");


输出:

{ iv: 'fb1f4b0a7daaada6cae678df32fad0f0',
  encryptedData: '41aad2618892aa3d1850d336ad15b50e' }

示例 2:

javascript

// Node.js program to demonstrate the    
// crypto.createCipheriv() 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 cipher
const cipher = crypto.createCipheriv(algorithm, key, iv);
 
// Declaring encrypted
let encrypted = '';
 
// Reading data
cipher.on('readable', () => {
  let chunk;
  while (null !== (chunk = cipher.read())) {
    encrypted += chunk.toString('base64');
  }
});
 
// Handling end event
cipher.on('end', () => {
console.log(encrypted);
});
 
// Writing data
cipher.write('CS-Portal');
cipher.end();
console.log("done");

输出:

done
MfHwhG/WPv+TIbG/qM78qA==

参考: https://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv_options