Re: Public Key Encryption with RSACryptoServiceProvider
From: Pieter Philippaerts (Pieter.nospam_at_mentalis.org)
Date: Fri, 25 Jun 2004 20:07:28 +0200
"AlBruAn" <AlBruAn@discussions.microsoft.com> wrote in message
> 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.
>From your description, there doesn't seem to be an abvious error. Could you
post the source code you're using so we can take a closer look?
> Even tho' I'm using the string "123" as my "license number" and using the
> public key, I'm getting different values for the encrypted "license
This is normal; whenever you encrypt something, the RSACryptoServiceProvider
will add a special padding to your data to make sure the length is equal to
the modulus size of your public key. This padding consists mostly of random
bytes, hence the difference.