我用RSACryptoServiceProvider做加解密的测试。
private void buttonCreateAsmKeys_Click(object sender, EventArgs e)
{
// Stores a key pair in the key container.
CspParameters cspp = new CspParameters(1);
cspp.KeyContainerName = keyName;
rsa = new RSACryptoServiceProvider(cspp);
rsa.PersistKeyInCsp = false;
if (rsa.PublicOnly == true)
label1.Text = String.Format("Key: {0} - Public Only", cspp.KeyContainerName);
else
label1.Text = String.Format("Key: {0} - Full Key Pair", cspp.KeyContainerName);
}
以上这段代码在按钮的响应事件中执行,rsa是类成员变量,初值为null。当这个程序启动后,什么也不干,只要连续点击按钮两次,然后关闭,如果在调试模式的话就会报告如下异常。
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll
Additional information: 密钥集不存在。
说明:rsa确实是连续两次被赋值。rsa.PersistKeyInCsp 为true时就没有以上问题。我是不想让系统保存这个RSA密钥,不知道该如何做?PersistKeyInCsp 为true时new两个同名的RSA是不是意味着引用同一个底层对象?
LHL