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
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
.
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 ;)
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.