Node.js crypto.privateEncrypt() Method Last Updated : 11 Oct, 2021 Comments Improve Suggest changes Like Article Like Report The crypto.privateEncrypt() method is used to encrypt the stated content of the buffer with the parameter 'privateKey'. Syntax: crypto.privateEncrypt( privateKey, buffer ) Parameters: This method accept two parameters as mentioned above and described below: privateKey: It can hold Object, string, Buffer, or KeyObject type of data. key: It is a 'PEM' encoded private key. It is of type string, Buffer, and KeyObject. passphrase: It is an optional passphrase for the private key which is either string or buffer. padding It is an optional padding value which is defined in crypto.constants, which can be crypto.constants.RSA_NO_PADDING, or crypto.constants.RSA_PKCS1_PADDING. It is of type crypto.constants. buffer: It contains Buffer, TypedArray, or DataView type of data. Return Value: It returns a new Buffer with the encrypted content. Below example illustrate the use of crypto.privateEncrypt() method in Node.js: Example 1: javascript // Node.js program to demonstrate the // crypto.privateEncrypt() 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: 520, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', cipher: 'aes-256-cbc', passphrase: '' } }); // Creating private key file fs.writeFileSync("private_key", keyPair.privateKey); } // Generate keys generateKeyFiles(); // Creating a function to encrypt string function encryptString (plaintext, privateKeyFile) { const privateKey = fs.readFileSync(privateKeyFile, "utf8"); // privateEncrypt() method with its parameters const encrypted = crypto.privateEncrypt( privateKey, Buffer.from(plaintext)); return encrypted.toString("base64"); } // Defining a text to be encrypted const plainText = "GfG"; // Defining encrypted text const encrypted = encryptString(plainText, "./private_key"); // Prints plain text console.log("Plaintext:", plainText); // Prints encrypted text console.log("Encrypted: ", encrypted); Output: Plaintext: GfG Encrypted: c60eR17GTQFkTI1ipTq5qFbYS58lIQqpDiou2UlYeOUE+u7agbtHvvwKaBpzBx4SvTCh5abpaqmyXCyGcUpGc7s= Example 2: javascript // Node.js program to demonstrate the // crypto.privateEncrypt() 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: 520, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', cipher: 'aes-256-cbc', passphrase: '' } }); // Creating private key file fs.writeFileSync("private_key", keyPair.privateKey); } // Generate keys generateKeyFiles(); // Creating a function to encrypt string function encryptString (plaintext, privateKeyFile) { const privateKey = fs.readFileSync(privateKeyFile, "utf8"); // privateEncrypt() method with its parameters const encrypted = crypto.privateEncrypt( privateKey, Buffer.from(plaintext)); // Returns buffer as its not encoded return encrypted; } // Defining a text to be encrypted const plainText = "GfG"; // Defining encrypted text const encrypted = encryptString(plainText, "./private_key"); // Prints plain text console.log("Plaintext:", plainText); // Prints encrypted text console.log("Encrypted buffer: ", encrypted); Output: Plaintext: GfG Encrypted buffer: <Buffer 14 e5 84 05 2f b5 59 64 22 d2 19 99 f9 66 e5 18 50 76 27 df 0b e6 9f 50 1d a2 51 6a 93 21 04 7b 8f 50 ba 11 82 fd 3c 6e c0 81 be 58 f9 d6 a6 c7 19 da ... > Reference: https://nodejs.org/api/crypto.html#crypto_crypto_privateencrypt_privatekey_buffer Comment More infoAdvertise with us N 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