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

后端 未结 3 521
北海茫月
北海茫月 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: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.

提交回复
热议问题