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 vector i.e., (iv)
The crypto.createDecipheriv()
method syntax is stated below:
crypto.createDecipheriv( algorithm, key, iv, options )
The above syntax accepts four major parameters that will be extensively explained as we go.
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
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
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
.
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.
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:
// 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));
{ iv: '7457f41085212607be55717d63243d86', encryptedData: 'dc88dd34a98d3d715db974c3e22326a8'} GeeksforGeeks
// 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");
done CS-Portal
Free Resources