How to create a pair private/public keys using Node.js crypto?

0 votes
I have to generate two keys (private and public) to encrypt a text with the public and let the user with the private key decrypt the text.

Is it possible with the module Crypto?
Oct 15 in Node-js by kartik
• 35,720 points
61 views

1 answer to this question.

0 votes

Hello @kartik,

nodejs v10.12 now supports this natively with crypto.generateKeyPair

const { generateKeyPair } = require('crypto');
generateKeyPair('rsa', {
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret'
  }
}, (err, publicKey, privateKey) => {
  // Handle errors and use the generated key pair.
});

Hope it works!!

answered Oct 15 by Niroj
• 77,920 points

Related Questions In Node-js

0 votes
1 answer

How to download a file with Node.js without using third-party libraries?

Hii, You can create an HTTP GET request and pipe ...READ MORE

answered 1 day ago in Node-js by Niroj
• 77,920 points
10 views
0 votes
1 answer

How to print a stack trace in Node.js?

Hello @kartik, To print stacktrace of Error in console in ...READ MORE

answered Jul 9 in Node-js by Niroj
• 77,920 points
65 views
0 votes
1 answer

How to create an HTTPS server in Node.js?

Hello @kartik, The minimal setup for an HTTPS ...READ MORE

answered Jul 13 in Node-js by Niroj
• 77,920 points
85 views
0 votes
1 answer

How to host a Node.Js application in shared hosting?

Hello @kartik, You can run node.js server on a typical ...READ MORE

answered Jul 17 in Node-js by Niroj
• 77,920 points
1,111 views
0 votes
1 answer

How to write files in Node.js?

Hello @kartik, Currently there are three ways to ...READ MORE

answered Jul 8 in Node-js by Niroj
• 77,920 points
97 views
0 votes
1 answer

How to Install a local module using npm?

Hello @kartik, This is what worked for me: npm ...READ MORE

answered Jul 9 in Node-js by Niroj
• 77,920 points
103 views
0 votes
1 answer

How to run Gulp tasks sequentially one after the other?

Hello @kartik, By default, gulp runs tasks simultaneously, ...READ MORE

answered Jul 12 in Node-js by Niroj
• 77,920 points
65 views
0 votes
1 answer

How to set environment variables from within package.json?

Hello @kartik, Set the environment variable in the ...READ MORE

answered Jul 13 in Node-js by Niroj
• 77,920 points
2,803 views
0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try: var fs = require('fs'); var dir = ...READ MORE

answered Jul 9 in Node-js by Niroj
• 77,920 points
540 views
0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try this: var fs = require('fs'); var dir ...READ MORE

answered Jul 20 in Node-js by Niroj
• 77,920 points
50 views