Node.js crypto.privateDecrypt() Method Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The crypto.privateDecrypt() method is used to decrypt the content of the buffer with privateKey.buffer which was previously encrypted using the corresponding public key, i.e. crypto.publicEncrypt(). Syntax: crypto.privateDecrypt( privateKey, buffer ) Parameters: This method accepts two parameters as mentioned above and described below: privateKey: It can hold an Object, string, Buffer, or KeyObject type of data.oaepHash It is the hash function of the typed string that is used for 'OAEP' padding. And the default value is 'sha1'.oaepLabel: It is the label that is used for 'OAEP' padding. And if it is not specified, then no label is used. It is of type Buffer, TypedArray, or DataView.padding: It is an optional padding value that is defined in crypto.constants, which can be crypto.constants.RSA_NO_PADDING, crypto.constants.RSA_PKCS1_PADDING, or crypto.constants.RSA_PKCS1_OAEP_PADDING. It is of type crypto.constants.buffer: It is of type Buffer, TypedArray, or DataView. Return Value: It returns a new Buffer with the decrypted content. The below example illustrates the use of crypto.privateDecrypt() method in Node.js: Example 1: javascript // Node.js program to demonstrate the // crypto.privateDecrypt() method // Including the crypto and fs modules const crypto = require('crypto'); const fs = require('fs'); // Reading the Public Key pubK = fs.readFileSync('pub.key').toString(); // Passing the text to be encrypted using private key const buf = Buffer.from('This is secret code', 'utf8'); // Encrypting the text secretData = crypto.publicEncrypt(pubK, buf); // Printing the encrypted text console.log(secretData); // Reading the Private key privK = fs.readFileSync('priv.key').toString(); // Decrypting the text using public key origData = crypto.privateDecrypt(privK, secretData); console.log(); // Printing the original content console.log(origData); Output: <Buffer 58 dd 76 8f cb 25 52 2b e7 3a b2 1b 0f 43 aa e0 df 65 fa 1d 3b 31 6f b7 f9 47 06 d5 f7 72 19 cd 2f 67 66 27 00 bb 43 8e 64 38 07 38 28 aa07 59 b4 60 ... > <Buffer 54 68 69 73 20 69 73 20 73 65 63 72 65 74 20 63 6f 64 65 > Example 2: javascript // Node.js program to demonstrate the // crypto.privateDecrypt() method // Including crypto and fs module const crypto = require('crypto'); const fs = require("fs"); // Using a function generateKeyFiles function generateKeyFiles() { const keyPair = crypto.generateKeyPairSync('rsa', { modulusLength: 530, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', cipher: 'aes-256-cbc', passphrase: '' } }); // Creating public and private key file fs.writeFileSync("public_key", keyPair.publicKey); fs.writeFileSync("private_key", keyPair.privateKey); } // Generate keys generateKeyFiles(); // Creating a function to encrypt string function encryptString(plaintext, publicKeyFile) { const publicKey = fs.readFileSync(publicKeyFile, "utf8"); // publicEncrypt() method with its parameters const encrypted = crypto.publicEncrypt( publicKey, Buffer.from(plaintext)); return encrypted.toString("base64"); } // Creating a function to decrypt string function decryptString(ciphertext, privateKeyFile) { const privateKey = fs.readFileSync(privateKeyFile, "utf8"); // privateDecrypt() method with its parameters const decrypted = crypto.privateDecrypt( { key: privateKey, passphrase: '', }, Buffer.from(ciphertext, "base64") ); return decrypted.toString("utf8"); } // Defining a text to be encrypted const plainText = "Geeks!"; // Defining encrypted text const encrypted = encryptString(plainText, "./public_key"); // Prints plain text console.log("Plaintext:", plainText); console.log(); // Prints buffer of encrypted content console.log("Encrypted Text: ", encrypted); console.log(); // Prints buffer of decrypted content console.log("Decrypted Text: ", decryptString(encrypted, "private_key")); Output: Plaintext: Geeks! Encrypted Text: ACks6H7InpaeGdI4w9MObyD73YB7N1V0nVsG5Jl10SNeH3no6gfgjeD4ZFsSFhCXzFkognMGbRNsg0BReVOHxRs7eQ== Decrypted Text: Geeks! Reference: https://nodejs.org/api/crypto.html#crypto_crypto_privatedecrypt_privatekey_buffer Comment More infoAdvertise with us Next Article Node.js crypto.privateDecrypt() Method nidhi1352singh Follow 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 Like