How can I decrypt RSA data in C # .NET appropriately?

My server creates RSACryptoServiceProvider and exports its parameters to a variable (RSAKeyInfo).

Then the public key is sent to the client, and the client encrypts something with this public key.

Now I need to be able to decrypt this data when sending back to the server - therefore, RSA is useful in my case.

However, I get a "Bad Data" exception when trying to recreate RSACryptoServiceProvider with imported parameters from the first first RSACryptoServiceProvider created earlier.

... The code may be more understandable.

Cryptography Creation:

class Cryptograph { public Cryptograph() { this.RSAKeyInfo = new RSACryptoServiceProvider(2048, new CspParameters(1)).ExportParameters(true); } } 

Access to it later for decryption:

 byte[] encrypted = ...; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(this.Cryptograph.RSAKeyInfo); byte[] decrypted = rsa.Decrypt(encrypted, false); Console.WriteLine(Utilities.ByteArrayToHexString(decrypted)); 

I get a "Bad Data" exception on this line:

 byte[] decrypted = rsa.Decrypt(encrypted, false); 

What am I doing wrong? How can I do it right? Thanks:)

PS: Please do not send MSDN or obvious links to Google results, I have read all these pages and still cannot get it to work.

+2
c # encryption rsa key
source share
2 answers

I needed encryption / decryption that did not use the add-on, and C # .NET does not provide it by default. OpenSSL.NET will do the job, however I am stuck trying to use it. ( See this question if you want to help me make it work ). :(

0
source share

When something is encrypted with a public key, you need to use the private key to decrypt it. I do not see where you are using the private key for decryption.

I understand that you have already read this, but you can read the "Encrypt" page and this "Decrypt" page and make sure that you are following these steps: http://msdn.microsoft.com/en-us/library/te15te69.aspx

If you are not encrypting very short messages, such as a password, RSA encryption should usually be used to encrypt a symmetric key, which encrypts / decrypts longer messages faster.

The size of what you can encrypt with the public key is tied to the length of the key.

+3
source share

All Articles