Skip to content

Instantly share code, notes, and snippets.

@nkhil
Created February 6, 2022 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkhil/3e6933edcc989a4de68001476b019c2a to your computer and use it in GitHub Desktop.
Save nkhil/3e6933edcc989a4de68001476b019c2a to your computer and use it in GitHub Desktop.
const fs = require('fs')
const crypto = require('crypto')
const dataToEncrypt = fs.readFileSync('data_to_encrypt.txt', { encoding: 'utf-8' })
const publicKey = Buffer.from(fs.readFileSync('public.pem', { encoding: 'utf-8' }))
const encryptedData = crypto.publicEncrypt(
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: 'sha256',
},
// We convert the data string to a buffer using `Buffer.from`
Buffer.from(dataToEncrypt)
)
fs.writeFileSync('encrypted_data.txt', encryptedData.toString('base64'), { encoding: 'utf-8' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment