Public Key Encryption with RSACryptoServiceProvider

From: AlBruAn (AlBruAn_at_discussions.microsoft.com)
Date: 06/25/04


Date: Fri, 25 Jun 2004 08:46:29 -0700

I'm attempting to use the RSACryptoServiceProvider class to encrypt some licensing information I'm storing in SQL Server, but I'm encountering a "Bad Data" CryptographicException when I try decrypting the data and wonder how I can get around this issue. I'm not storing any information in SQL Server until I can get the mechanics of encryption working properly. Anyway...

I've generated a pair of public and private keys using RSACryptoServiceProvider's ToXml function, passing false and true arguments, respectively, to it. I then persist these keys to the root directory of my C: drive using an XmlTextWriter.

When it's time to encrypt a license number, I create a new instance of the RSACryptoServiceProvider object and read in the public key using its FromXml function. I then call the Encrypt function, passing it the license number and specifying no OAEP padding. My final step is to persist the encrypted values to a separate file in the root directory of my C: drive. Even tho' I'm using the string "123" as my "license number" and using the same public key, I'm getting different values for the encrypted "license number". This, I'm sure, is a factor in getting the Bad Data message when I attempt to decypher a "license number".

If anyone can point me in the right direction, I'd greatly appreciate it.

Regards,

Allen