I've a client side which is running on C# and to encrypt the data with RSA public key and send over to Nodejs to decrypt. Everything works almost perfect except the data I decrypted is come with weird padding. I've tried all ways I've known before i ask here, so any help will be appreciated.
C#
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("{\"hello\":\"world\"}");
using (var stream = File.OpenRead("C:\\rsaKeys\\my-public-key.pem"))
using (var reader = new PemReader(stream))
{
var rsaParameters = reader.ReadRsaKey();
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(rsaParameters);
encryptedData = RSA.Encrypt(dataToEncrypt, false);
}
return encryptedData;
}
NodeJs
const buffer = Buffer.from(val, 'base64');
const decrypted = crypto.privateDecrypt(
{
key: this.rsaPrivateKey,
padding: crypto.constants.RSA_PKCS1_PADDING
},buffer);
let rtn = decrypted.toString('utf8');
the decrypted data will become { " h e l l o " : " w o r l d " } instead. Any idea why would this happen?