node-rsa errors when trying to decrypt message with private key

后端 未结 3 508
北海茫月
北海茫月 2020-12-16 05:38

So I\'ve been trying to use node with node-rsa and javascript with jsencrypt to create a website (for an assignment) where the javascript client gets the public key generate

相关标签:
3条回答
  • 2020-12-16 06:10

    I had the same issue.

    encrypt.setOptions({encryptingScheme:'pkcs1'});  //Can be 'pkcs1_oaep' or 'pkcs1'. Default 'pkcs1_oaep'.
    

    But, it still failed.

    I have changed the lib from node-rsa to ursa, like this:

    privateKey.decrypt(thirdEncrypted, 'base64', 'utf8',ursa.RSA_PKCS1_PADDING);
    

    The problem has been resolved in ursa.

    0 讨论(0)
  • 2020-12-16 06:12

    To answer your question @Curious_Programmer be default node-rsa uses pkcs1_oaep for encryption and decryption while jsencrypt uses pkcs1. Thankfully node lets you change the encryptionScheme, what you need to do is add ...

    myDecrypter.setOptions({encryptionScheme: 'pkcs1'});
    

    under

    var myDecrypter = new NodeRSA({b: 512});
    

    and all will work like a charm, I hoped I helped you ;)

    0 讨论(0)
  • 2020-12-16 06:36

    It seems that the ciphertext is a buffer, i.e. binary data. Then it is transported using JSON, which consists of text. You need to use a text encoding over the binary data to transport it over a text based interface.


    Check the following definition of the encrypt method:

    key.encrypt(buffer, [encoding], [source_encoding]);
    

    with the reminder that the default is 'buffer' for [encoding].

    So you should be using:

    var encrypted = myEncrypter.encrypt(message, 'base64', 'utf-8');
    

    where 'base64' is for the ciphertext encoding and 'utf-8' is for the plaintext encoding.


    The decryption routine should automatically use base64 decoding of the ciphertext:

    var clearMessage = myDecrypter.decrypt(message.message, 'utf8');
    

    should be just fine.

    0 讨论(0)
提交回复
热议问题