I need to encrypt some text with RSA, and then recover it later using the private key. My problem is that RSACryptoServiceProvider.Encrypt()
outputs a different value every time, even when using the same key. Here is my code which I put into LINQpad to test:
CspParameters cp = new CspParameters();
cp.KeyContainerName = "MyKey";
cp.Flags = CspProviderFlags.UseMachineKeyStore | CspProviderFlags.UseExistingKey;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
// using LINQpad to verify the key is loaded properly -- same every time
rsa.ToXmlString(true).Dump();
byte[] rgb = new ASCIIEncoding().GetBytes("Hello world");
byte[] xx = rsa.Encrypt(rgb, false);
string b64 = Convert.ToBase64String(xx);
// this changes every time:
b64.Dump();
I'm guessing that the class must be using something else as well as the key to affect the output, but I'm struggling to find out what.