Skip to content

Instantly share code, notes, and snippets.

@nkhil
Created February 6, 2022 22:05
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/d2baf07f99ed53702df332c34115566d to your computer and use it in GitHub Desktop.
Save nkhil/d2baf07f99ed53702df332c34115566d to your computer and use it in GitHub Desktop.
const crypto = require('crypto')
const fs = require('fs')
const encryptedData = fs.readFileSync('encrypted_data.txt', { encoding: 'utf-8' })
const privateKey = fs.readFileSync('private.pem', { encoding: 'utf-8' })
const decryptedData = crypto.privateDecrypt(
{
key: privateKey,
// In order to decrypt the data, we need to specify the
// same hashing function and padding scheme that we used to
// encrypt the data in the previous step
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: 'sha256',
},
Buffer.from(encryptedData, 'base64'),
)
fs.writeFileSync('decrypted_data.txt', decryptedData.toString('utf-8'), { encoding: 'utf-8' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment