Skip to content

Instantly share code, notes, and snippets.

@imaman
Created September 20, 2020 05:19
Show Gist options
  • Save imaman/6cb43f7ad91be868ffdb0c766846ea57 to your computer and use it in GitHub Desktop.
Save imaman/6cb43f7ad91be868ffdb0c766846ea57 to your computer and use it in GitHub Desktop.
node crypto public/private keys
var keys = {}
crypto.generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
}
}, (err, publicKey, privateKey) => {
if (err) return console.error('err', err.stack)
keys = {publicKey, privateKey}
var cipherText = crypto.publicEncrypt(keys.publicKey, Buffer.from('abc')).toString('hex')
console.log('cipherText=' + cipherText)
var plainText = crypto.privateDecrypt(keys.privateKey, Buffer.from(cipherText, 'hex')).toString()
console.log('plainText=' + plainText)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment