What is the createDecipheriv() method in Node.js?

What do we mean by encryption and decryption?

The technique of transforming a normal communication (plaintext) into a meaningless message is known as encryption (ciphertext).

Decryption, on the other hand, is the process of returning a meaningless communication (ciphertext) to its original form (plaintext).

widget

Now, bringing the above understanding into Node.js…

The crypto module of Node.js includes the crypto.createDeCipheriv() method.

Decrypting encrypted texts is the responsibility of the decipher class. You must encrypt the information before sending it securely to another developer. The only way for the information to be read is for the receiver to decrypt it. decipher is a class that does exactly that.

The crypto.createDecipheriv() method is a crypto module’s built-in application programming interface for creating a Decipher object with the specified parameters: algorithm, key, and the iv (known as the initialization vector, which is responsible for making the cipher uncertain and unique).

Syntax of crypto.createDeCipheriv():

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

Parameters

The crypto.createDecipheriv() function takes in three arguments. Parameters of the crypto.createDecipheriv() are defined as follows:

  • algorithm: This accepts data for the algorithm that will be used to generate the cipher. The following are some examples of possible values: aes192, aes256, and so on.

  • key: This accepts input for the algorithm’s raw key as well as iv. The types of values that can be used are string, buffer, TypedArray, and DataView. It can be a type object of secret type, if desired.

  • iv: also known as the initialization vector. This parameter accepts iv as input, resulting in a cipher that is both uncertain and unique. It doesn’t have to be kept a secret. String, buffer, TypedArray, and DataView are all viable value types. If the cipher does not require it, this can be null.

  • options: This is a parameter that can be used to control the stream’s behavior. When the cipher is used in CCM or OCB mode (like ‘aes-256-ccm’), this is not an option.

Return value

The crypto.createDecipheriv() method returns a Decipher object.

Example

Here is an example to enhance your understanding of the use of crypto.createDecipheriv() method in Node:

// Import module into your application
const crypto = require('crypto');
const algorithm = 'aes-192-cbc';
const password = 'Password that was used to generate key';
// We will first generate the key, as it is dependent on the algorithm.
// In this case for aes192, the key is 24 bytes (192 bits).
// We will use the async `crypto.scrypt()` instead for deciphering.
const key = crypto.scryptSync(password, 'salt', 24);
// The IV is usually passed along with the ciphertext.
const iv = Buffer.alloc(16, 0); // Initialization vector.
// Create decipher with key and iv
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = '';
decipher.on('readable', () => {
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString('utf8');
}
});
decipher.on('end', () => {
console.log(decrypted);
// Prints: some clear text data
});
// Encrypted with same algorithm, key and iv.
const encrypted =
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
decipher.write(encrypted, 'hex');
decipher.end();

Free Resources