我理解的.net Framework 2.0 RSA加密2011-12-21 博客园 肖蕾这里我必须总结一下,因为RSACryptoServiceProvider 引发的异常快使我疯掉了!代码:
WindowsIdentity wi = Logon(UserName, Password);
if (null != wi) {
WindowsImpersonationContext wic = wi.Impersonate();
CspParameters cp = new CspParameters();
cp.ProviderType = 1;
cp.Flags = CspProviderFlags.UseMachineKeyStore;
cp.KeyNumber = (int)KeyNumber.Exchange;
cp.KeyContainerName = "KEY_1";
RSACryptoServiceProvider csp = new RSACryptoServiceProvider( cp);
this.txtPublicKey.Text = this.PublicKey = csp.ToXmlString(false);
csp.Clear();
wic.Undo();
我最先遇到:System.Security.Cryptography.CryptographicException,发生位置是aspnet_wp.exe(1128)----------->windows XPSystem.Security.Cryptography.CryptographicException,发生位置是w3wp.exe------------------>windows Server 2003症状:是我用了加密算法后,把应用挂在IIS上,就时不时报如上错。解决:while impersonated and dont clean up the keys of the RSACryptoProvider using the clear method while impersonated.解决步骤:ImpersonateuserCreate a new RSACryptoServiceProvider (pair)Encrypt or Decrypt your dataCall clear to release the keys (pair.Clear())Undo impersonation