Decipher.final() Method in Node.js


The decipher.final() is used to return a buffer or string containing the value of decipher object. It is one of the inbuilt method that is provided by the class Cipher within the crypto module. The decipher method cannot be used to decrypt data once the decipher.final method has been called. Calling the cipher.final method more than once will throw an error.

Syntax

decipher.final([outputEncoding])

Parameters

The above parameters are described as below −

  • outputEncoding – It takes the output encoding as a parameter. The input type for this parameter is string. Possible input values are hex, base64, etc.

Example

Create a file with name – decipherFinal.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 decipherFinal.js

decipherFinal.js

// Example to demonstrate the use of cipher.final() method

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

// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678123456789';

// Retrieving key for the decipher object
const key = crypto.scryptSync(password, 'old data', 24);

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

const decipher = crypto.createDecipheriv(algorithm, key, iv);

// Initializing the cipher object to get cipher
const encrypted1 =
'a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a';
// const encrypted2 = '8d11772fce59f08e7558db5bf17b3112';

let decryptedValue1 = decipher.update(encrypted1, 'hex', 'utf8');
// let decryptedValue1 = decipher.update(encrypted1, 'hex', 'utf8');

decryptedValue1 += decipher.final('utf8');

// Printing the result...
console.log("Decrypted value -- " + decryptedValue1);
// console.log("Base64 String:- " + base64Value)

Output

C:\home
ode>> node decipherFinal.js Decrypted value -- Welcome to tutorials point

Example

Let's take a look at one more example.

// Example to demonstrate the use of cipher.final() method

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

// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678123456789';

// Retrieving key for the decipher object
const key = crypto.scryptSync(password, 'old data', 24);

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

const decipher = crypto.createDecipheriv(algorithm, key, iv);

// Initializing the cipher object to get cipher
const encrypted =
'a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a';
// const encrypted2 = '8d11772fce59f08e7558db5bf17b3112';

var buf = [];

// Updating the decopher data
let decrypted = decipher.update(encrypted, 'hex', 'utf8');

// Pushinf the data into buffer after decryption
buf.push(decrypted);
buf.push(decipher.final('utf8'));

// Printing the result
console.log(buf.join(' '));

Output

C:\home
ode>> node decipherFinal.js Welcome to tutor ials point

Updated on: 20-May-2021

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements