Node.js | crypto.createCipheriv() Method

The crypto.createCipheriv() method is an inbuilt application programming interface of the crypto module which is used to create a Cipher object, with the stated algorithm, key and initialization vector (iv).

Syntax:

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

Parameters: This method accept four parameters as mentioned above and described below:

  • algorithm: It is a string type value that dependent on OpenSSL. The examples are aes192, aes256, etc.
  • key: It is the raw key which is used by the algorithm and iv. It holds the string, Buffer, TypedArray or DataView. The key can be a KeyObject optionally of type secret.
  • iv: It is an initialization vector that must be uncertain and very unique. However, an ideal iv will be cryptographically random. It don’t need to be secret. It can holds string, Buffer, TypedArray, or DataView type data. If cipher doesn’t requires iv then it can be null.
  • options: It is an optional parameter that is used to control stream behavior. It is optional except when a cipher is used in CCM or OCB mode(e.g. ‘aes-128-ccm’). In that case, the authTagLength option is required which defines the length(bytes) of the authentication tag whereas, in GCM mode, the authTagLength option is not needed but it can be used to set the length of the authentication tag that will be returned by the getAuthTag() method and the default value is 16 bytes.

Return Value: It returns Cipher object.

Below examples illustrate the use of crypto.createCipheriv() method in Node.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");

chevron_right


Output:

done
MfHwhG/WPv+TIbG/qM78qA==

Reference: https://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv_options




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.