Open In App
Related Articles

Node.js crypto.createDecipheriv() Method

Improve Article
Save Article
Like Article

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


crypto.createDecipheriv( 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 Decipher object. 

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

Example 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 = {
    decrypted += chunk.toString('utf8');
// Handling end event
decipher.on('end', () => {
// Encrypted data which is to be decrypted
const encrypted =
decipher.write(encrypted, 'base64');



Example 2: 


// Node.js program to demonstrate the    
// crypto.createDecipheriv() 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,]);
 // 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,]);
 // returns data after decryption
 return decrypted.toString();
// Encrypts output
var output = encrypt("GeeksforGeeks");
// Decrypts output


{ iv: '6bbc47a2756d6d6bf315cfd3cc0b711a',  encryptedData: 'fae9a6fb31c0b0668da8c3be1b1da81a' }


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 17 Jan, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials