Welcome

首页 / 软件开发 / .NET编程技术 / 我理解的.net Framework 2.0 RSA加密

我理解的.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 XP

System.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.

解决步骤:

Impersonateuser

Create a new RSACryptoServiceProvider (pair)

Encrypt or Decrypt your data

Call clear to release the keys (pair.Clear())

Undo impersonation