Open In App

Node.js decipher.final() Method

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The decipher.final() method is an inbuilt application programming interface of class Decipher within crypto module which is used to return the buffer containing the value of decipher object.

Syntax:

const decipher.final([outputEncoding])

Parameters: This method takes the output encoding as a parameter.

Return Value: This method return the object of buffer containing the deciphered value.

Example 1: In this example, we will use decipher.final() method

Filename: index.js

Javascript




// Node.js program to demonstrate the
// decipher.final() method
 
// Importing crypto module
const crypto = require('crypto');
 
// Creating and initializing algorithm and password
const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
 
// Getting key for the decipher object
const key = crypto.scryptSync(password, 'salt', 24);
 
// Creating and initializing the static iv
const iv = Buffer.alloc(16, 0);
 
// Creating and initializing the decipher object
const decipher = crypto.createDecipheriv(algorithm, key, iv);
 
// Encrypted using same algorithm, key and iv.
const encrypted =
    'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
 
// Updating the data to the decipher
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
 
// Deciphering data by using
// final() method
decrypted += decipher.final('utf8');
 
// Display the result
console.log(decrypted);


Output:

some clear text data

Example 2: In this example, we will use decipher.final() method

Filename: index.js

JavaScript




// Node.js program to demonstrate the
// decipher.final() method
 
// Importing crypto module
const crypto = require('crypto');
 
// Creating and initializing algorithm and password
const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
 
// Getting key for the decipher object
const key = crypto.scryptSync(password, 'salt', 24);
 
// creating and initializing the static iv
const iv = Buffer.alloc(16, 0);
 
// creating and initializing the decipher object
const decipher = crypto.createDecipheriv(algorithm, key, iv);
 
// Encrypted using same algorithm, key and iv.
const encrypted =
    'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
 
// Creating and initializing empty buffer
let buf = [];
 
// Updating the data to the decipher
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
 
// Pushing the updated data into buffer
buf.push(decrypted);
 
// Pushing decrypted data into buffer
buf.push(decipher.final('utf8'));
 
// Display the result
console.log(buf.join(' '));


Output:

some clear text  data

Run the index.js file using the following command:

node index.js

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/crypto.html#crypto_decipher_final_outputencoding



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads