I use generateKeyPair to create public key and private key
const { generateKeyPair } = crypto
generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret'
}
}, (err, publicKey, privateKey) => {
// Handle errors and use the generated key pair.
if (err) throw err;
fs.writeFileSync("key.pem", privateKey)
fs.writeFileSync("public.pem", publicKey)
res.json({ publicKey, privateKey })
});
and I use public key to encrypt,it works but when I use the privatekey to decrypt,it comes an error the code is
fs.readFile("key.pem", "utf8", (err, data) => {
const content = crypto.privateDecrypt(data, Buffer.from(c, "base64"))//c is the cipherText and the error happends here
})
error message is :
return method(data, format, type, passphrase, buffer, padding, oaepHash,
TypeError: Passphrase required for encrypted key
so I don't understand what's that mean,
bcuz the API is :crypto.privateDecrypt(privateKey, buffer)
and when I generate it , I have passed the 'passphrase',
so I have no idea about it
I ran into a very similar issue. You have to use the function privateDecrypt in the following manner
var decrypted = crypto.privateDecrypt({
key: privateKey.toString(),
passphrase: 'top secret',
}, buffer);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With