Created
September 20, 2020 05:19
-
-
Save imaman/6cb43f7ad91be868ffdb0c766846ea57 to your computer and use it in GitHub Desktop.
node crypto public/private keys
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
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