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:
const crypto = require( 'crypto' );
const fs = require( "fs" );
function generateKeyFiles() {
const keyPair = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 520,
publicKeyEncoding: {
type: 'spki' ,
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8' ,
format: 'pem' ,
cipher: 'aes-256-cbc' ,
passphrase: ''
}
});
fs.writeFileSync( "private_key" , keyPair.privateKey);
}
generateKeyFiles();
function encryptString (plaintext, privateKeyFile) {
const privateKey = fs.readFileSync(privateKeyFile, "utf8" );
const encrypted = crypto.privateEncrypt(
privateKey, Buffer.from(plaintext));
return encrypted.toString( "base64" );
}
const plainText = "GfG" ;
const encrypted = encryptString(plainText, "./private_key" );
console.log( "Plaintext:" , plainText);
console.log( "Encrypted: " , encrypted);
|
Output:
Plaintext: GfG
Encrypted: c60eR17GTQFkTI1ipTq5qFbYS58lIQqpDiou2UlYeOUE+u7agbtHvvwKaBpzBx4SvTCh5abpaqmyXCyGcUpGc7s=
Example 2:
const crypto = require( 'crypto' );
const fs = require( "fs" );
function generateKeyFiles() {
const keyPair = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 520,
publicKeyEncoding: {
type: 'spki' ,
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8' ,
format: 'pem' ,
cipher: 'aes-256-cbc' ,
passphrase: ''
}
});
fs.writeFileSync( "private_key" , keyPair.privateKey);
}
generateKeyFiles();
function encryptString (plaintext, privateKeyFile) {
const privateKey = fs.readFileSync(privateKeyFile, "utf8" );
const encrypted = crypto.privateEncrypt(
privateKey, Buffer.from(plaintext));
return encrypted;
}
const plainText = "GfG" ;
const encrypted = encryptString(plainText, "./private_key" );
console.log( "Plaintext:" , plainText);
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Oct, 2021
Like Article
Save Article