0

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

  1. I generated two Keys(public and private).
  2. Save both keys.
  3. Then I import those keys and call to encrypt and decrypt methods in NodeRSA module.
  4. Perfectly work both ways.

Backend

  1. Generated two keys.

  2. Keep it.

  3. Use them and call to encrypt and decrypt methods in RSACryptoServiceProvider.

  4. 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?

suranga upul
  • 211
  • 3
  • 6
  • Java has a different padding option than c#. Padding is used on key to add random bytes to key so actual key size is hidden. – jdweng Apr 29 '20 at 17:41
  • @jdweng: there is no Java anywhere in this question. – President James K. Polk Apr 29 '20 at 19:04
  • `crypherText.Replace(" ", "+")` What is going on here? I don't know of any base64 encoding that uses a space as an encoding character. Also, I don't believe that particular XML format is what RSACryptoServiceProvider is expecting. Also, according to the documentation I read, Node RSA uses OAEP padding by default. If so, then this `csp.Decrypt(dataBytes, false);` needs to be changed to `csp.Decrypt(dataBytes, true);` – President James K. Polk Apr 29 '20 at 19:12
  • @PresidentJamesMoveonPolk If While I am passing encrypted text from frontend to backend I am getting error ["Invalid length for a Base-64 char array or string."](https://stackoverflow.com/a/2925959/3962409) that's why I used `crypherText.Replace(" ", "+")` . XML part I used to add my private key to RSACryptoServiceProvider instance. I will try again to add this by PEM. I tried by changing my code into `csp.Decrypt(dataBytes, true);` but same error occurred. – suranga upul Apr 30 '20 at 01:26
  • It sounds like your data is getting corrupted in transit. I can't help you with that, sorry. – President James K. Polk Apr 30 '20 at 03:05

0 Answers0