Home > Web Front-end > JS Tutorial > decipher.update() method in Node.js

decipher.update() method in Node.js

PHPz
Release: 2023-08-25 08:13:16
forward
1151 people have browsed it

Node.js 中的 decipher.update() 方法

decipher.update() is used to update the decryption with the received data according to the given encoding format. It is one of the built-in methods provided by the Decipher class in the crypto module. If the input encoding is specified, the data parameter is a string, otherwise the data parameter is a buffer

Syntax

decipher.update(data, [inputEncoding], [outputEncoding])
Copy after login

Parameters

The above parameters are described as follows-

  • data – It requires data as input passed to update the decrypted content.

  • inputEncoding - It takes the input encoding as a parameter. Possible input values ​​are hexadecimal, base64, etc.

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

Example

Create a file named decipherUpdate.js and copy the following code snippet. After creating the file, run this code using the following command as shown in the example below -

node decipherUpdate.js
Copy after login

decipherUpdate.js

// Example to demonstrate the use of decipher.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 decipher object to get decipher
const encrypted = '083bfe1b2f91677e5d00add115be2f1b2e362e190406f5c6b60e86969bf03bff';
// const encrypted2 = '8d11772fce59f08e7558db5bf17b3112';

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

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

// Printing the result...
console.log("Decrypted value -- " + decryptedValue);
// console.log("Base64 String:- " + base64Value)
Copy after login

Output

C:\homeode>> node decipherUpdate.js
Decrypted value -- Some new text data
Copy after login

Example

Let’s look at another example.

// Example to demonstrate the use of decipher.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
crypto.scrypt(password, 'salt', 24,
   { N: 512 }, (err, key) => {
      if (err) throw err;

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

   // Initializing the decipher with algo, key and iv
   const decipher = crypto.createDecipheriv(algorithm, key, iv);
   const encrypted = '91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074';

   //Getting the decrypted string value
   const decrypted = decipher.update(encrypted, 'hex', 'utf8');

   // Printing the result...
   console.log("Decrypted value:- " + decrypted);
});
Copy after login

Output

C:\homeode>> node decipherUpdate.js
Decrypted value:- Some new text data
Copy after login

The above is the detailed content of decipher.update() method in Node.js. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template