What is crypto.createDecipheriv(algorithm, key, iv[, options])?

Definition of crypto.createDecipheriv()

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

The crypto.createDecipheriv() method syntax is stated below:

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

Parameters

The above syntax accepts four major parameters that will be extensively explained as we go.

  • The first parameter is the algorithm. A programming algorithm is a procedure or formula used for solving a problem.

An algorithm is based on the conduction of a sequence of specified actions that describe how to do something so that your computer will do it exactly that way every time. In the crypto.createDecipheriv() method context, an algorithm is a string type value that is dependent on OpenSSL. The examples are aesAdvanced Encryption Standard 192, aes256, etc. AES, also known as 256-bit AES, is a symmetric key cipher used for both encryption and decryption, meaning the receiver and the sender of the data need a copy of the key.

An OpenSSL is a robust, commercial-grade, full-featured toolkit for the Transport Layer Security (TLS). TLS is a cryptography that uses encryptions to protect the communicationsdata and information dissemination within a computer system.

  • The second parameter is the key. A key is a sort of information processed through a cryptographic algorithm that can encodelock or decodeunlock cryptographic data and functions, including authentication, authorization, and encryption.

Keys are strings of letters or numbers that are stored in a file. It is the raw key that is used by the algorithm and IV. The raw key contains the buffer, TypedArray, and string. A buffer is an area of memory that represents a fixed-size chunk of memory. The DataView function in JavaScript provides an interface to read and write more than one number type into an ArrayBuffer.

  • The third parameter is the IV. An initialization vector (IV) is an input to a cryptographic primitive used along with a secret key for encrypting information.

An IV is used to avert repetitions in data encryption, which makes it difficult for hackers, who use dictionary attacks, to find a way to break a cipher.

The initialization vector must be unpredictable and unique, but it doesn’t need to be secret. It can hold string, buffer, TypedArray, or DataView type data. If the cipher doesn’t require an IV, it can be null.

  • The fourth parameter is Options. It is an optional parameter that is used to control stream behavior, however, it is not optional when a cipher is used in a CCM. CCM is an authenticated encryption algorithm designed to provide both authentication and confidentiality.

Return value

A return value returns a Decipher object

The codes below are examples to illustrate and summarize the use of the crypto.createDecipheriv() method in Node.js:

Code example 1

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

Output:

{ iv: '7457f41085212607be55717d63243d86', encryptedData: 'dc88dd34a98d3d715db974c3e22326a8'} GeeksforGeeks

Code example 2

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

Output:

done CS-Portal

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved