decipher.update() Method in Node.js

The decipher.update() method is used to decrypt encrypted data incrementally in Node.js. It processes encrypted data according to specified encoding formats and is part of the Decipher class within the crypto module.

Syntax

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

Parameters

  • data - The encrypted data to be decrypted. Can be a string or Buffer depending on inputEncoding.

  • inputEncoding - (Optional) The encoding of the input data. Common values: 'hex', 'base64'. If omitted, data is treated as a Buffer.

  • outputEncoding - (Optional) The encoding for the output. Common values: 'utf8', 'hex', 'base64'. If omitted, returns a Buffer.

Return Value

Returns the decrypted data as a string (if outputEncoding specified) or Buffer (if no outputEncoding).

Example 1: Basic Decryption with Synchronous Key Generation

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

// Algorithm and password setup
const algorithm = 'aes-192-cbc';
const password = '12345678123456789';

// Generate key using scryptSync
const key = crypto.scryptSync(password, 'old data', 24);

// Initialize static IV
const iv = Buffer.alloc(16, 0);

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

// Encrypted data (hex format)
const encrypted = '083bfe1b2f91677e5d00add115be2f1b2e362e190406f5c6b60e86969bf03bff';

// Decrypt using update method
let decryptedValue = decipher.update(encrypted, 'hex', 'utf8');

// Finalize the decryption
decryptedValue += decipher.final('utf8');

console.log("Decrypted value: " + decryptedValue);
Decrypted value: Some new text data

Example 2: Asynchronous Key Generation

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

// Algorithm and password setup
const algorithm = 'aes-192-cbc';
const password = '12345678123456789';

// Generate key asynchronously
crypto.scrypt(password, 'salt', 24, { N: 512 }, (err, key) => {
    if (err) throw err;

    // Initialize static IV
    const iv = Buffer.alloc(16, 0);

    // Create decipher object
    const decipher = crypto.createDecipheriv(algorithm, key, iv);
    
    // Encrypted data
    const encrypted = '91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074';

    // Decrypt the data
    const decrypted = decipher.update(encrypted, 'hex', 'utf8');

    console.log("Decrypted value: " + decrypted);
});
Decrypted value: Some new text data

How It Works

The decipher.update() method works in conjunction with decipher.final():

  • update() processes the main encrypted data
  • final() handles any remaining encrypted data and padding
  • Both methods' outputs must be concatenated for complete decryption

Key Points

  • Always call decipher.final() after update() to complete decryption
  • Input encoding determines how the encrypted data is interpreted
  • Output encoding determines the format of the returned decrypted data
  • The same algorithm, key, and IV used for encryption must be used for decryption

Conclusion

The decipher.update() method is essential for decrypting data in Node.js applications. It provides flexible encoding options and works seamlessly with decipher.final() to ensure complete and secure data decryption.

Updated on: 2026-03-15T23:19:00+05:30

454 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements