crypto.createCipheriv() Method in Node.js

The crypto.createCipheriv() method will first create and then return the cipher object as per the algorithm passed for the given key and authorization factor (iv).
crypto.createCipheriv(algorithm, key, iv, options)
The above parameters are described as below −
algorithm – It takes the input for the algorithm that would be used to create the cipher. Some possible values are: aes192, aes256, etc.
algorithm – It takes the input for the algorithm that would be used to create the cipher. Some possible values are: aes192, aes256, etc.
key – It takes input for the raw key that is used by the algorithm and iv. Possible values can be of type: string, buffer, TypedArray or DataView. It can optionally be a type object of secret type.
key – It takes input for the raw key that is used by the algorithm and iv. Possible values can be of type: string, buffer, TypedArray or DataView. It can optionally be a type object of secret type.
iv – Also known as the initialization vector. This parameter takes input for iv that will make the cipher uncertain and unique. It does not need to be a secret. Its possible value types are: string, buffer, TypedArray, DataView. This can be null if not needed by the cipher.
iv – Also known as the initialization vector. This parameter takes input for iv that will make the cipher uncertain and unique. It does not need to be a secret. Its possible value types are: string, buffer, TypedArray, DataView. This can be null if not needed by the cipher.
options – This is an optional parameter for controlling the stream behaviour. This is not optional when cipher is used in CCM or OCB mode (Like 'aes-256-ccm')
options – This is an optional parameter for controlling the stream behaviour. This is not optional when cipher is used in CCM or OCB mode (Like 'aes-256-ccm')
Create a file with name – createCipheriv.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −
node createCipheriv.js
createCipheriv.js
 Live Demo
// A node demo program for creating the ECDH

// Importing the crypto module
const crypto = require('crypto');

// Initializing the algorithm
const algorithm = 'aes-256-cbc';

// Initializing the key
const key = crypto.randomBytes(32);

// Initializing the iv vector
const iv = crypto.randomBytes(16);

// Creating the function to encrypt data
function encrypt(text) {

// Creating the cipher with the above defined parameters
let cipher = crypto.createCipheriv(
   'aes-256-cbc', Buffer.from(key), iv);

let encrypted = cipher.update(text);

encrypted = Buffer.concat([encrypted, cipher.final()]);

// Returning iv and the encrypted data
return { iv: iv.toString('hex'),
   encryptedData: encrypted.toString('hex') };
}
// Printing public & private curve keys...
var output = encrypt("TutorialsPoint");
console.log(output);
C:\home\node>> node createCipheriv.js
{ iv: '3dd899aa441c00d4d8d2ff95abb2e684',
encryptedData: 'b4985053bc1507fc25a4d99823dc8b03' }
Let's take a look at one more example.
// A node demo program for creating the ECDH

// Importing the crypto module
const crypto = require('crypto');

// Initializing the algorithm
const algorithm = 'aes-192-cbc';

// Defining and initializing the password
const password = '123456789'

// Initializing the key
const key = crypto.scryptSync(password, 'TutorialsPoint', 24);

// Initializing the iv vector
const iv = Buffer.alloc(16, 0);

// Creating the cipher with the above defined parameters
const cipher = crypto.createCipheriv(algorithm, key, iv);

let encrypted = '';

// Reading and encrypting the data
cipher.on('readable', () => {
   let chunk;
   while (null !== (chunk = cipher.read())) {
      encrypted += chunk.toString('base64');
   }
});

//Handling the closing/end event
cipher.on('end', () => {
   console.log(encrypted);
});

// Printing public & private curve keys...
cipher.write('TutorialsPoint');
cipher.end();
console.log("Completed... !");
C:\home\node>> node createCipheriv.js
Completed... !
uqeQEkXy5dpJjQv+JDvMHw==