Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Node crypto.createCipheriv() Method
Next article icon

Node.js crypto.createDecipheriv() Method

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The crypto.createDecipheriv() method is an inbuilt application programming interface of crypto module which is used to create a Decipher object, with the stated algorithm, key and initialization vector i.e, (iv). 

Syntax:

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

Parameters: This method accept four parameters as mentioned above and described below:

  • algorithm: It is a string type value that dependent on OpenSSL. The examples are aes192, aes256, etc.
  • key: It is the raw key which is used by the algorithm and iv. It holds the string, Buffer, TypedArray or DataView. The key can be a KeyObject optionally of type secret.
  • iv: It is an initialization vector that must be uncertain and very unique. However, an ideal iv will be cryptographically random. It don't need to be secret. It can holds string, Buffer, TypedArray, or DataView type data. If cipher doesn't requires iv then it can be null.
  • options: It is an optional parameter that is used to control stream behavior. It is optional except when a cipher is used in CCM or OCB mode(e.g. 'aes-128-ccm'). In that case, the authTagLength option is required which defines the length(bytes) of the authentication tag whereas, in GCM mode, the authTagLength option is not needed but it can be used to set the length of the authentication tag that will be returned by the getAuthTag() method and the default value is 16 bytes.

Return Value: It returns Decipher object. 

Below examples illustrate the use of crypto.createDecipheriv() method in Node.js: 

Example 1: 

javascript
// Node.js program to demonstrate the      // crypto.createDecipheriv() method  // Includes crypto module const crypto = require('crypto');  // Defining algorithm const algorithm = 'aes-192-cbc';  // Defining password const password = 'bncaskdbvasbvlaslslasfhj';  // Defining key const key = crypto.scryptSync(password, 'GfG', 24);  // Defininf iv const iv = Buffer.alloc(16, 0);   // Creating decipher const decipher =      crypto.createDecipheriv(algorithm, key, iv);  // Declaring decrypted let decrypted = '';  // Reading data decipher.on('readable', () => {   let chunk;   while (null !== (chunk = decipher.read())) {     decrypted += chunk.toString('utf8');   } });  // Handling end event decipher.on('end', () => { console.log(decrypted); });  // Encrypted data which is to be decrypted const encrypted =   'MfHwhG/WPv+TIbG/qM78qA==';  decipher.write(encrypted, 'base64'); decipher.end();  console.log("done"); 

Output:

done CS-Portal

Example 2: 

javascript
// Node.js program to demonstrate the      // crypto.createDecipheriv() method  // Includes crypto module const crypto = require('crypto');  // Defining algorithm const algorithm = 'aes-256-cbc';  // Defining key const key = crypto.randomBytes(32);  // Defining iv const iv = crypto.randomBytes(16);  // An encrypt function function encrypt(text) {   // Creating Cipheriv with its parameter  let cipher =      crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);   // Updating text  let encrypted = cipher.update(text);   // Using concatenation  encrypted = Buffer.concat([encrypted, cipher.final()]);   // Returning iv and encrypted data  return { iv: iv.toString('hex'),      encryptedData: encrypted.toString('hex') }; }  // A decrypt function function decrypt(text) {   let iv = Buffer.from(text.iv, 'hex');  let encryptedText =     Buffer.from(text.encryptedData, 'hex');   // Creating Decipher  let decipher = crypto.createDecipheriv(         'aes-256-cbc', Buffer.from(key), iv);   // Updating encrypted text  let decrypted = decipher.update(encryptedText);  decrypted = Buffer.concat([decrypted, decipher.final()]);   // returns data after decryption  return decrypted.toString(); }  // Encrypts output var output = encrypt("GeeksforGeeks"); console.log(output);  // Decrypts output console.log(decrypt(output)); 

Output:

{ iv: '6bbc47a2756d6d6bf315cfd3cc0b711a',  encryptedData: 'fae9a6fb31c0b0668da8c3be1b1da81a' } GeeksforGeeks

Reference: https://nodejs.org/api/crypto.html#crypto_crypto_createdecipheriv_algorithm_key_iv_options


Next Article
Node crypto.createCipheriv() Method

N

nidhi1352singh
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • Node.js-crypto-module

Similar Reads

    Node.js cipher.final() Method
    The cipher.final() method in Node.js is used to signal to the cipher object that the encryption or decryption process is complete. This method must be called after all data has been passed to the cipher object using the cipher.update() method. The cipher.final() method returns the remaining encrypte
    2 min read
    Node.js cipher.update() Method
    The cipher.update() method is an inbuilt application programming interface of class Cipher within crypto module which is used to update the cipher with data according to the given encoding format. Syntax: const cipher.update(data[, inputEncoding][, outputEncoding]) Parameters: This method takes the
    2 min read
    Node.js crypto.getCiphers() Method
    The crypto.getCiphers() method returns an array the names of all the supported cipher algorithms. Syntax: crypto.getCiphers() Parameters: This method doesn't accepts any parameters. Return Value: It returns the names of all the supported cipher algorithms. Below example illustrate the use of crypto.
    2 min read
    Node.js crypto.createECDH() Method
    The crypto.createECDH() method is an inbuilt application programming interface of crypto module which is used to create an Elliptic Curve Diffie-Hellman i.e, (ECDH) key exchange object with the help of a predefined curve which is defined by the curveName string. Moreover you can use crypto.getCurves
    2 min read
    Node.js crypto.createDecipheriv() Method
    The crypto.createDecipheriv() method is an inbuilt application programming interface of crypto module which is used to create a Decipher object, with the stated algorithm, key and initialization vector i.e, (iv).  Syntax: crypto.createDecipheriv( algorithm, key, iv, options ) Parameters: This method
    3 min read
    Node crypto.createCipheriv() Method
    The crypto.createCipheriv() method is an inbuilt application programming interface of the crypto module which is used to create a Cipher object, with the stated algorithm, key, and initialization vector (iv).Syntax: crypto.createCipheriv( algorithm, key, iv, options )Parameters: This method accepts
    2 min read
    Node.js crypto.getDiffieHellman() Method
    The crypto.getDiffieHellman() method is used to create a predefined DiffieHellmanGroup key exchange object. Here, the favored groups are 'modp1', 'modp2', 'modp5', which are defined in RFC 2412 and 'modp14', 'modp15', 'modp16', 'modp17', 'modp18', defined in RFC 3526. Syntax: crypto.getDiffieHellman
    2 min read
    Node.js crypto.pbkdf2() Method
    The crypto.pbkdf2() method gives an asynchronous Password-Based Key Derivation Function 2 i.e. (PBKDF2) implementation. Moreover, a particular HMAC digest algorithm which is defined by digest is implemented to derive a key of the required byte length (keylen) from the stated password, salt, and iter
    2 min read
    Node crypto.createHash() Method
    The crypto.createHash() method is used to create a Hash object that can be used to create hash digests by using the stated algorithm. Syntax:crypto.createHash( algorithm, options )Parameters: This method accepts two parameters as mentioned above and described below:algorithm: It is dependent on the
    2 min read
    Node.js crypto.createHmac() Method
    The crypto.createHmac() method is used to create an Hmac object that uses the stated 'algorithm' and 'key'.Syntax:crypto.createHmac( algorithm, key, options )Parameters: This method accepts three parameters as mentioned above and described below:algorithm: It is dependent on the accessible algorithm
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences