0

I am trying to send a encrypted response to client's api using public key and trying to decrypt the response that comes out of the client using my private key. When i try to decrypt the message, it says "Key does not exist". Below are the codes that I am using.

public string Encryption(string strText, string publickey)
{
  var data = Encoding.UTF8.GetBytes(strText);
  using (var rsa = new RSACryptoServiceProvider(2048))
  {
    try
    {
      var key = "<RSAKeyValue><Modulus>" + publickey.Replace('-', '+').Replace(' ', '+') + "</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
      rsa.FromXmlString(key);
      var encryptedData = rsa.Encrypt(data, true);
      var base64Encrypted = Convert.ToBase64String(encryptedData);
      return base64Encrypted;
    }
    catch (Exception ex)
    {
      return ex.Message;
    }
    finally
    {
      rsa.PersistKeyInCsp = false;
    }
  }
}

And for Decryption

  public string Decrypt(string data, string privateKey)
{
  CspParameters cp = new CspParameters();
  cp.KeyContainerName = "MyKeyContainerName";
  var rsa = new RSACryptoServiceProvider(cp);
  var dataArray = data.Split(new char[] { ',' });
  byte[] dataByte = new byte[dataArray.Length];
  dataByte = Encoding.UTF8.GetBytes(data);
  var encoder = new UnicodeEncoding();
  var key = "<RSAKeyValue><Modulus>" + privateKey + "</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
  rsa.FromXmlString(key);
  var decryptedByte = rsa.Decrypt(dataByte, false);
  return encoder.GetString(decryptedByte);
}

Hope this is enough. Please advice

1 Answers1

1

You're trying to perform a series of transformations, but you're not doing the opposite thing in each direction.

You've taken the encrypted binary data and converted it to base64, but then you're taking the base64 data and converting it back to binary using UTF-8, after splitting it by commas for some reason:

var dataArray = data.Split(new char[] { ',' });
byte[] dataByte = new byte[dataArray.Length];
dataByte = Encoding.UTF8.GetBytes(data);

To reverse the last operation of the base64-encoding, you should be performing a base64-decoding:

byte[] dataByte = Convert.FromBase64String(data);

That may not be the only thing wrong, but it's the first thing I spotted.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi. Thanks for the response. When i tried the above mentioned change, I am getting the error as "Invalid length for a Base-64 char array or string". This is because the data that i get from server is just "SE". Also can you suggest me on Configuring RSACryptoServiceProvider, with key as string rather than a XML. I googled it a lot, yet not able to get a clear idea on it. – Vivek Jeeva Kumar Apr 01 '18 at 16:19
  • @VivekJeevaKumar: It's worth focusing on one thing at a time - let's sort out the decryption first, then you can ask a separate question about how to configure the crypto provider. (I'll remove that from my answer now.) So, if you're getting data of just "SE" from the server, that's *not* the value returned by your `Encryption` method, because it's not a complete base64-string. You need to look at what `Encryption` returned, and why you're not getting that data to your client. I'd also advise trying to build a console app that *just* called `Encryption` then `Decrypt`. Remove the server part. – Jon Skeet Apr 01 '18 at 16:25
  • Thanks. So this how the flow is right now. I send data to the client API(encrypted). The client API will send me the response(Results of my call), which i have to decrypt using private key. Response from the client API is not controlled by me. – Vivek Jeeva Kumar Apr 01 '18 at 16:32
  • @VivekJeevaKumar: Again, I'd strongly recommend that you get encryption and decryption working in a very, very simple situation first - just a console app, where you can call `string encrypted = Encryption("Hello world", publicKey); string decrypted = Decrypt(encrypted, privateKey);`. Once you've got that part working, you can work on how this integrates with the server. *Until* you've got that part working, you're going to have a much harder time understanding what's wrong in the bigger picture. – Jon Skeet Apr 01 '18 at 16:35
  • Thanks, let me try that out first. – Vivek Jeeva Kumar Apr 01 '18 at 16:41