Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
crypto.createDecipheriv() Method in Node.js
The crypto.createCipheriv() is a programming interface from the 'crypto' module. It will create and return the Decipher object as per the given algorithm, key, iv and options passed in the function.
Syntax
crypto.createDecipheriv(algorithm, key, iv, [options])
Parameters
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.
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.
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')
Example
Create a file with name – createDecipheriv.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 createDecipheriv.js
createDecipheriv.js
// 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 Decipher with the above defined parameters
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = '';
// Reading and encrypting the data
decipher.on('readable', () => {
let chunk;
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString('utf8');
}
});
//Handling the closing/end event
decipher.on('end', () => {
console.log(decrypted);
});
// Encrypted data which is going to be decrypted
const encrypted = 'uqeQEkXy5dpJjQv+JDvMHw==';
// Printing the decrypted text
decipher.write(encrypted, 'base64');
decipher.end();
console.log("Completed... !");
Output
C:\home
ode>> node createDecipheriv.js Completed... ! TutorialsPoint
Example
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-256-cbc';
// Defining and initializing the password
const password = '123456789'
// Initializing the key
const key = crypto.randomBytes(32);
// Initializing the iv vector
const iv = crypto.randomBytes(16);
// Encrypt function to encrypt the data
function encrypt(text) {
// Creating the cipher with the above defined parameters
let cipher =
crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
// Updating the encrypted text...
let encrypted = cipher.update(text);
// Using concatenation
encrypted = Buffer.concat([encrypted, cipher.final()]);
// Returning the iv vector along with the encrypted data
return { iv: iv.toString('hex'),
encryptedData: encrypted.toString('hex') };
}
//Decrypt function for decrypting the data
function decrypt(text) {
let iv = Buffer.from(text.iv, 'hex');
let encryptedText =
Buffer.from(text.encryptedData, 'hex');
// Creating the decipher from algo, key and iv
let decipher = crypto.createDecipheriv(
'aes-256-cbc', Buffer.from(key), iv);
// Updating decrypted text
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
// returning response data after decryption
return decrypted.toString();
}
// Encrypting the below data and printing output
var output = encrypt("Welcome to TutorialsPoint !");
console.log("Encrypted data -- ", output);
//Printing decrypted data
console.log("Decrypted data -- ", decrypt(output));
Output
C:\home
ode>> node createDecipheriv.js Encrypted data -- { iv: '3fb2c84290e04d9bfb099bc65a7ac941', encryptedData: '4490777e90c5a78037cb92a99d561ae250562e2636af459b911cfa01c0191e3f' } Decrypted data -- Welcome to TutorialsPoint !