RSACryptoServiceProvider
-
Hi, I am trying to use RSACryptoServiceProvider in my ASP.NET application to access keys from a MachineKeyStore on my computer, running windows xp and IIS 5. I created machinekeystore like following in Visual Studio 2005 Command Prompt: aspnet_regiis -pc "CustomKeys" -exp (command was successful) Then I executed following command because I am impersonating my web application with a non-default user: aspnet_regiis -pa "CustomKeys" "domain\auserforapplication" (command was successful) Then I worte the following code:
public partial class Examples_EncryptionExample : System.Web.UI.Page { CspParameters CspParam; string publicXmlString = string.Empty; string privateXmlString = string.Empty; protected void Page_Load(object sender, EventArgs e) { try { byte [] encrypted; string decrypted; UnicodeEncoding ByteConverter = new UnicodeEncoding(); encrypted = EncrptData("data to encrypt"); Response.Write(System.Text.Encoding.Unicode.GetString(encrypted)); decrypted = DecryptData(encrypted); Response.Write(decrypted); } catch (Exception ex) { } } public string DecryptData(byte [] data) { RSACryptoServiceProvider RsaCsp; byte[] decryptedData; RsaCsp = new RSACryptoServiceProvider(); RsaCsp.FromXmlString(privateXmlString); decryptedData = RsaCsp.Decrypt(data, false); return System.Text.Encoding.Unicode.GetString(decryptedData); } public byte [] EncrptData(string data) { RSACryptoServiceProvider RsaCsp; RSACryptoServiceProvider RsaCsp2; UnicodeEncoding ByteConverter = new UnicodeEncoding(); CspParam = new CspParameters(); CspParam.KeyContainerName = "CustomKeys"; CspParam.Flags = CspProviderFlags.UseMachineKeyStore; byte[] encryptedData = ByteConverter.GetBytes(data); RsaCsp = new RSACryptoServiceProvider(CspParam); //Getting public key publicXmlString = RsaCsp.ToXmlString(false); //Getting private key privateXmlString = RsaCsp.ToXmlString(true); RsaCsp2 = new RSACryptoServiceProvider(); RsaCsp2.FromXmlString(publicXmlString); encryptedData = RsaCsp2.Encrypt(System.Text.Encoding.Unicode.GetBytes(data), false); return encryptedData; } }
The problem over here is that when ever I try t