Open In App

Node.js decipher.update() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The decipher.update() method is an inbuilt application programming interface of class Decipher within crypto module which is used to update the decipher with the data according to the given encoding format.

Syntax:

const decipher.update(data[, inputEncoding][, outputEncoding])

Parameters: This method takes the following parameter:

  • data: It is used to update the decipher by new content.
  • inputEncoding: Input encoding format.
  • outputEncoding: Output encoding format.

Return Value: This method does not return any value.

Example 1: Filename: index.js

Javascript




// Node.js program to demonstrate the
// decipher.update() 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 into decipher object
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
 
// Getting decrypted data
decrypted += decipher.final('utf8');
 
// Display the result
console.log("buffer :- " + decrypted);


Output:

buffer :- some clear text data

Example 2: Filename: index.js

Javascript




// Node.js program to demonstrate the
// decipher.update() 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 cipher object
crypto.scrypt(password, 'salt', 24,
 
    { N: 512 }, (err, key) => {
 
        if (err) throw err;
 
        // 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 into decipher object
        const decrypted = decipher.update(encrypted, 'hex', 'utf8');
 
        console.log("buffer :- " + decipher);
    });


Output:

buffer :- [object Object]

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_update_data_inputencoding_outputencoding



Last Updated : 03 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads