Created
February 6, 2022 22:01
-
-
Save nkhil/3e6933edcc989a4de68001476b019c2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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