Skip to content

Instantly share code, notes, and snippets.

@nkhil
Created February 6, 2022 21:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nkhil/6b3a9180d292d8b6adc1d2ddee3dc407 to your computer and use it in GitHub Desktop.
const crypto = require('crypto')
const fs = require('fs')
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
// The standard secure default length for RSA keys is 2048 bits
modulusLength: 2048,
})
// *********************************************************************
//
// To export the public key and write it to file:
const exportedPublicKeyBuffer = publicKey.export({ type: 'pkcs1', format: 'pem' })
fs.writeFileSync('public.pem', exportedPublicKeyBuffer, { encoding: 'utf-8' })
// *********************************************************************
// *********************************************************************
//
// To export the private key and write it to file
const exportedPrivateKeyBuffer = privateKey.export({ type: 'pkcs1', format: 'pem' })
fs.writeFileSync('private.pem', exportedPrivateKeyBuffer, { encoding: 'utf-8' })
// *********************************************************************
@Lau2802
Copy link

Lau2802 commented Jul 15, 2022

Hello!,

I have a question: When do you send the file that you want the public key and the private key to be encrypted? where is the path of the file to be encrypted?

Thanks you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment