My task is to encrypt data in frontend(reactJS) and decrypt it in the backend (.net core 2.0). I am using NodeRSA library in fronted and RSACryptoServiceProvider in the backend.
Front end
- I generated two Keys(public and private).
- Save both keys.
- Then I import those keys and call to encrypt and decrypt methods in NodeRSA module.
- Perfectly work both ways.
Backend
Generated two keys.
Keep it.
Use them and call to encrypt and decrypt methods in RSACryptoServiceProvider.
Awesome.
Then I use the public key which created by the frontend and generates the encrypted string. then I pass it to the backend and try to decrypt. but I got the error "The parameter is incorrect" But when I used frontend public and private keys for encrypting and decrypt methods in the backend. those keys are working fine.
This is how I am trying
Frontend (reactJS)
const key_public = new NodeRSA({ b: 1024 });
key_public.importKey(public_key);
let encryptedText = key_public.encrypt(this.state.secret, 'base64');
console.log(encryptedText);
Backend (.net core 2.0)
public string DecryptFromMyKey(string crypherText)
{
var dataBytes = Convert.FromBase64String(crypherText.Replace(" ", "+"));
string privateXmlString = "<?xml version=\"1.0\" encoding=\"utf-16\"?>....";
RSACryptoServiceProviderExtensions.FromXmlString(csp, privateXmlString);
var plainText = csp.Decrypt(dataBytes, false);
var final = Encoding.Unicode.GetString(plainText);
//string myKey = ExportPublicKeyToPEMFormat(csp, publicXmlString);
return final;
}
I check my keys and encrypted values with this site. https://8gwifi.org/rsafunctions.jsp. according to this site, my backend methods are doing their jobs correctly. It shows the correct plain text. but when I try it the encrypted text which created by the frontend. It not work.
Have I missed any coding part here? Is there any algorithm to add for frontend code?