crypto.createCipheriv() Method in Node.js

The crypto.createCipheriv() method creates and returns a cipher object using the specified algorithm, key, and initialization vector (IV). This method is essential for encrypting data in Node.js applications.

Syntax

crypto.createCipheriv(algorithm, key, iv, options)

Parameters

The parameters are described below:

  • algorithm - The encryption algorithm to use (e.g., 'aes-256-cbc', 'aes-192-cbc')

  • key - The raw key used by the algorithm. Can be string, Buffer, TypedArray, or DataView

  • iv - Initialization vector that makes encryption unique. Can be string, Buffer, TypedArray, or DataView. May be null for some ciphers

  • options - Optional parameter for controlling stream behavior. Required for CCM or OCB modes

Example 1: Basic Encryption Function

Create a file named createCipheriv.js and run it with node createCipheriv.js:

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

// Initializing the algorithm
const algorithm = 'aes-256-cbc';

// Initializing the key
const key = crypto.randomBytes(32);

// Initializing the iv vector
const iv = crypto.randomBytes(16);

// Creating the function to encrypt data
function encrypt(text) {
    // Creating the cipher with the above defined parameters
    let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
    
    let encrypted = cipher.update(text);
    encrypted = Buffer.concat([encrypted, cipher.final()]);
    
    // Returning iv and the encrypted data
    return { 
        iv: iv.toString('hex'),
        encryptedData: encrypted.toString('hex') 
    };
}

// Encrypting sample text
var output = encrypt("TutorialsPoint");
console.log(output);
{ iv: '3dd899aa441c00d4d8d2ff95abb2e684',
  encryptedData: 'b4985053bc1507fc25a4d99823dc8b03' }

Example 2: Stream-based Encryption

This example demonstrates using cipher as a stream with event handlers:

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

// Initializing the algorithm
const algorithm = 'aes-192-cbc';

// Defining and initializing the password
const password = '123456789'

// Initializing the key using scryptSync
const key = crypto.scryptSync(password, 'TutorialsPoint', 24);

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

// Creating the cipher with the above defined parameters
const cipher = crypto.createCipheriv(algorithm, key, iv);

let encrypted = '';

// Reading and encrypting the data
cipher.on('readable', () => {
    let chunk;
    while (null !== (chunk = cipher.read())) {
        encrypted += chunk.toString('base64');
    }
});

// Handling the closing/end event
cipher.on('end', () => {
    console.log(encrypted);
});

// Writing data to cipher and ending
cipher.write('TutorialsPoint');
cipher.end();
console.log("Completed... !");
Completed... !
uqeQEkXy5dpJjQv+JDvMHw==

Key Points

  • The IV should be unique for each encryption operation
  • Key length must match the algorithm requirements
  • Stream-based approach is useful for large data
  • Always handle the 'end' event when using streams

Conclusion

The crypto.createCipheriv() method provides flexible encryption capabilities in Node.js. Use it with proper key management and unique IVs for secure data encryption.

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

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements