You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varchildProcess=require('child_process');varcrypto=require('crypto');varpk;varmess_chiffre;varsign=function(){varbuf=newBuffer("Coucou c'est relou!!!");varsig=crypto.privateEncrypt(pk,buf);mess_chiffre=sig.toString('hex');console.log("Signed : ",mess_chiffre);};vardecrypt=function(){varbuff=newBuffer(mess_chiffre,'hex')console.log('Buffer : ',buff)console.log('decrypting withh pk: ')console.log(pk)varmess=crypto.privateDecrypt(pk,buff)console.log(mess)};childProcess.exec('openssl rsa -in private.key',{},function(err,stdout,stderr){if(err)throwerr;pk=stdout;// Save in memory for later usesign();decrypt();});
constcrypto=require('crypto');constprivateKey=`-----BEGIN RSA PRIVATE KEY-----MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQXGukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63ilAkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlFL0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5kX6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2eplU9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=-----END RSA PRIVATE KEY-----`;constpublicKey=`-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQAB-----END PUBLIC KEY-----`;constplaintext=Buffer.from('Hello world!','utf8');// This is what you usually do to transmit encrypted data.constenc1=crypto.publicEncrypt(publicKey,plaintext);constdec1=crypto.privateDecrypt(privateKey,enc1);console.log(dec1.toString('utf8'));// This works as well.constenc2=crypto.privateEncrypt(privateKey,plaintext);constdec2=crypto.publicDecrypt(publicKey,enc2);console.log(dec2.toString('utf8'));
Reacted by Krish, Kote Isaev, Pham Ba Thong and Pedro Leoni
@Nolaan That's what the private key is usually for, yes, and publicEncrypt / privateDecrypt is much more common than the other pair of functions. However, some asymmetric ciphers (such as RSA) can be used in the other way as well.
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:
Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error at Object.privateDecrypt (node:internal/crypto/cipher:79:12) at Object.<anonymous> (/home/runner/Generate-Key-Pair/index.js:27:30) at Module._compile (node:internal/modules/cjs/loader:1101:14)
I am currently using repl.it for running Node JS code and Chrome Debugger console for running client side code.
Activity
addaleax commentedon Sep 10, 2018
@nodejs/crypto
tniessen commentedon Sep 10, 2018
Just so we are on the same page: Why are you trying to use
privateDecryptafter applyingprivateEncrypt? That's not how asymmetric encryption works.mscdex commentedon Sep 10, 2018
Yes, either
privateEncrypt()needs to bepublicEncrypt()orprivateDecrypt()needs to bepublicDecrypt().mscdex commentedon Sep 10, 2018
Also, in the future please ask help questions such as this on the nodejs/help issue tracker.
addaleax commentedon Sep 10, 2018
I do think this should be mentioned in the docs, ideally with an example of how to do it “right”?
Nolaan commentedon Sep 10, 2018
@tniessen So please provide a working example. From the docs I couldn't neither with publicEncrypt/privateDecrypt or otherwise.
tniessen commentedon Sep 10, 2018
Nolaan commentedon Sep 10, 2018
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?
Nolaan commentedon Sep 10, 2018
I found the answer. TIL : https://security.stackexchange.com/questions/9957/can-i-use-a-private-key-as-a-public-key-and-vice-versa
tniessen commentedon Sep 10, 2018
@Nolaan That's what the private key is usually for, yes, and
publicEncrypt/privateDecryptis much more common than the other pair of functions. However, some asymmetric ciphers (such as RSA) can be used in the other way as well.Nolaan commentedon Sep 10, 2018
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).
tniessen commentedon Sep 10, 2018
@Nolaan You can by passing the private key to
publicDecrypt/publicEncrypt.doc: improve asymmetric crypto docs
doc: improve asymmetric crypto docs
doc: improve asymmetric crypto docs
doc: improve asymmetric crypto docs
doc: improve asymmetric crypto docs
popov654 commentedon Sep 17, 2023
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:
Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error at Object.privateDecrypt (node:internal/crypto/cipher:79:12) at Object.<anonymous> (/home/runner/Generate-Key-Pair/index.js:27:30) at Module._compile (node:internal/modules/cjs/loader:1101:14)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?
popov654 commentedon Sep 18, 2023
I also tried decrypting with no padding, it gave no errors but the result is garbage (not correct):
AlastairTaft commentedon Sep 21, 2023
@popov654
I think you're missing
oaepHashpopov654 commentedon Sep 21, 2023
Thanks, that helped