-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
crypto.privateDecrypt cannot decrypt message #22792
Comments
@nodejs/crypto |
Just so we are on the same page: Why are you trying to use |
Yes, either |
Also, in the future please ask help questions such as this on the nodejs/help issue tracker. |
I do think this should be mentioned in the docs, ideally with an example of how to do it “right”? |
@tniessen So please provide a working example. From the docs I couldn't neither with publicEncrypt/privateDecrypt or otherwise. |
const crypto = require('crypto');
const privateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX
GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il
AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF
L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k
X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl
U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ
37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=
-----END RSA PRIVATE KEY-----`;
const publicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0
FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/
3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQAB
-----END PUBLIC KEY-----`;
const plaintext = Buffer.from('Hello world!', 'utf8');
// This is what you usually do to transmit encrypted data.
const enc1 = crypto.publicEncrypt(publicKey, plaintext);
const dec1 = crypto.privateDecrypt(privateKey, enc1);
console.log(dec1.toString('utf8'));
// This works as well.
const enc2 = crypto.privateEncrypt(privateKey, plaintext);
const dec2 = crypto.publicDecrypt(publicKey, enc2);
console.log(dec2.toString('utf8')); |
Thanks! But why would the private key be able to decrypt anything? Isn't it suppose to be just an encrypting key, the one you send to people? |
I found the answer. TIL : https://security.stackexchange.com/questions/9957/can-i-use-a-private-key-as-a-public-key-and-vice-versa |
@Nolaan That's what the private key is usually for, yes, and |
Ok, I've lost it a bit in the docs because I saw :
So I thought I could just use the private key for symmetric encryption (encrypt/decrypt). |
@Nolaan You can by passing the private key to |
I have the exactly same problem. I created a pair of keys on server side with Node, transfered one to the client, encrypted a sample string, but when I try to decrypt it on the server, I get the following:
I am currently using repl.it for running Node JS code and Chrome Debugger console for running client side code. Client side code is this:
Node JS code is this:
What am I missing? |
I also tried decrypting with no padding, it gave no errors but the result is garbage (not correct):
|
I think you're missing const decryptedData = crypto.privateDecrypt(
{
key: privateKey,
oaepHash: 'SHA256'
},
Buffer.from(data, 'base64')
); |
Thanks, that helped |
I'm trying to encrypt/decrypt data using a rsa key but the process fails somehow with this message :
Code :
The text was updated successfully, but these errors were encountered: