Skip to content

Instantly share code, notes, and snippets.

@yaodong
Last active November 17, 2019 22:14
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 yaodong/f71eec4edf468f6ae9091bc39dfa3661 to your computer and use it in GitHub Desktop.
Save yaodong/f71eec4edf468f6ae9091bc39dfa3661 to your computer and use it in GitHub Desktop.
const fs = require('fs')
const crypto = require('crypto')
const publicKey = fs.readFileSync('public.pem')
const privateKey = fs.readFileSync('private.pem')
const message = 'Hello, world!'
const encryptedMsg = crypto.publicEncrypt(publicKey, Buffer.from(message, 'utf8'))
console.log('encrypted by private key: ' + encryptedMsg.toString('hex'))
const decryptedMsg = crypto.privateDecrypt(privateKey, encryptedMsg);
console.log('decrypted by public key: ' + decryptedMsg.toString('utf8'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment