I am trying to encrypt and decrypt data using RSA. Ultimately, I need to be able to place an existing public key into the RSACryptoServiceProvider and encrypt using that, but for now, I'd settle for any success at all.
I have this simple test. I assure you that the LoadFromFile() and SaveToFile() methods are working, and the raw data is a valid byte array with the correct data in it. The line (as indicated below) which encrypts data throws an exception, stating "unspecified error".
I get this behavior on two machines (Windows XP Pro SP2 and Windows XP Home SP2), but not on a third machine (Windows 2000 Pro). What must I do to get it working?
Any ideas are welcome.
private void button2_Click(object sender, EventArgs e)
{
// Load the raw data to be encrypted.
byte[] lOriginal = LoadFromFile(@"c:\firstfile.txt");
// Create an encryptor with new keys
RSACryptoServiceProvider lRSA = new RSACryptoServiceProvider();
// Encrypt the raw data.
//This line throws CryptographicException: "Unspecified Error"
byte[] lEncrypted = lRSA.Encrypt(lOriginal, false);
// Store the encrypted data.
SaveTofile(@"c:\encrypted.txt", lEncrypted);
}