Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 593
Dail Avatar asked Dec 15 '11 13:12

Dail


People also ask

How do I generate a public key from a private key?

To generate an SSH private/public key pair for your use, you can use the ssh-keygen command-line utility. You can run the ssh-keygen command from the command line to generate an SSH private/public key pair. If you are using Windows, by default you may not have access to the ssh-keygen command.

What is private public key pair?

The public key is used to encrypt messages and the private key is used to decrypt messages. The reverse is done to create a digital signature. Only the owner of the key pair knows the private key, but everyone can know the public key.

What are public and private crypto keys?

The public key is used to send cryptocurrency into a wallet. The private key is used to verify transactions and prove ownership of a blockchain address. If someone sends you, say one bitcoin (BTC), a private key will be required to “unlock” that transaction and prove that you are now the owner of that bitcoin.


1 Answers

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. }); 
like image 175
Nelson Owalo Avatar answered Oct 06 '22 12:10

Nelson Owalo