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.
Lazlo
source share