System.Security.Cryptography.CryptographicException:键集不存在 [英] System.Security.Cryptography.CryptographicException: keyset does not exist

查看:741
本文介绍了System.Security.Cryptography.CryptographicException:键集不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我把X509证书encypt和decypt消息时,我得到了一些错误信息,无法修复此问题。可能有人曾经happend解决这个bug?谢谢



说明:在当前Web请求的执行过程中发生未处理的异常。请检查堆栈跟踪有关该错误它起源于代码的更多信息和。



异常详细信息:




System.Security.Cryptography.CryptographicException:键集不。不存在



源错误:



行53:使用(的RSACryptoServiceProvider
rsaProviderDecrypt =
(的RSACryptoServiceProvider)cerDecrypt.PublicKey.Key)线路54:结果
{55号线:plainHashBytes =
rsaProviderDecrypt.Decrypt(encryptedHashBytes,FALSE);第56行:结果
rsaProviderDecrypt.Clear(); 57号线:结果
rsaProviderDecrypt.Dispose();



源文件:
E:\PayUSite\PayMvcApp\Controllers\\ \\HashMessageController.cs行:55



堆栈跟踪:



[CryptographicException:键集不存在。 ]结果
System.Security.Cryptography.CryptographicException.ThrowCryptographicException(的Int32
小时)+41结果
System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle
pKeyContext ,字节] pbEncryptedKey,的Int32 cbEncryptedKey,布尔
fOAEP,ObjectHandleOnStack ohRetDecryptedKey)+ 0结果
System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(字节[]
三原色,布尔fOAEP) 579




源代码:

 字符串DOCFILE =使用Server.Mappath(〜/ DOCX / DirectAccess_StepByStep.doc); 
哈希的HashAlgorithm = HashAlgorithm.Create(SHA1);
字节[] hashedBytes;
使用(的FileStream FS =新的FileStream(DOCFILE,FileMode.Open))
{
//计算消息的散列值
hashedBytes = hash.ComputeHash(FS);
hash.Dispose();
fs.Close();
}

串hashedString = Convert.ToBase64String(hashedBytes);

//加密信息摘要
串priKeyFile =使用Server.Mappath(〜/证书/ WosMiddle.pfx​​);
X509Certificate2 certEncrypt =新X509Certificate2(priKeyFile,123456);
字节[] encryptedHashBytes;使用(的RSACryptoServiceProvider rsaProviderEncrypt =(的RSACryptoServiceProvider)certEncrypt.PrivateKey)
{
encryptedHashBytes = rsaProviderEncrypt.Encrypt(hashedBytes,FALSE)
;
rsaProviderEncrypt.Dispose();
}

//解密的消息摘要
串pubKeyFile =使用Server.Mappath(〜/证书/ WosMiddle-pubkey.cer);
X509Certificate2 cerDecrypt =新X509Certificate2(pubKeyFile);
字节[] plainHashBytes;使用(的RSACryptoServiceProvider rsaProviderDecrypt =(的RSACryptoServiceProvider)cerDecrypt.PublicKey.Key)
{

// ***将在这里抛出的错误消息... ***
plainHashBytes = rsaProviderDecrypt .Decrypt(encryptedHashBytes,FALSE);
rsaProviderDecrypt.Dispose();
}

//验证消息是否被修改过
串docFile2 =使用Server.Mappath(〜/ DOCX / DirectAccess_StepByStep.doc);
HASH2的HashAlgorithm = HashAlgorithm.Create(SHA1);
字节[] hashedBytes2;使用
(FS2的FileStream =新的FileStream(docFile2,FileMode.Open))
{
//计算消息的散列值
hashedBytes2 = hash.ComputeHash(FS2);
fs2.Close();
}

//比较哈希值
的isEqual布尔= plainHashBytes.SequenceEqual(hashedBytes2);


解决方案

这个问题是旧的,但有人寻找解决方案同时继续使用加密解密这里是我如何管理来解决这个错误:



该基地是我的证书已安装通过双击该 .PFX 文件并选择商店。

$走错了路b
$ b

的错误方式安装证书



1 的双击该证书:





2 的向导打开,单击下一步按钮:





3 的向导显示证书的位置,单击下一个按钮:





4 的输入密码,然后点击下一步。





5 的选择存储然后点击下一步





6。的向导显示证书信息,请点击完成按钮





7 的更迭对话框显示:





所以,在这一点上,我有错误的键集不存在






要解决,我继续这种方式(正确的方法)



1 的执行Microsoft管理控制台(程序mmc.exe):





2 的一个空白MMC实例表明:





3 的点击文件 - >添加/删除管理单元...



添加管理单元



4 的选择证书管理单元中的添加按钮的点击





5 的选择的 计算机帐户 的然后单击下一步按钮



$ ; b
$ b

6 选择的 本地计算机 的然后点击完成按钮:



$ b $ ; b

7 的证书管理单元现在被添加,点击确定按钮:





< 。EM> 8 的选择个人存储,然后右键单击并选择导入





9。的浏览证书,并点击下一步:





10 的输入密码,然后单击下一步按钮:





11 的自动选择证书存储区:



自动选择商店



12 的证书信息显示:





13 的成功对话框消息显示:





14 的刷新MMConsole显示证书:





15。 的右键点击证书,然后点击管理私钥...





16 的添加池标识或在IIS用户在我来说,我补充IIS_IUSRS:





17 的用户已添加,点击确定按钮:





而且它完成密钥集做的存在现在!


When I make x509 certificate to encypt and decypt message, I got some error information and could not fix this problem. Could someone ever happend to solve this bug? thanks.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:

System.Security.Cryptography.CryptographicException: keyset does not exist。

Source Error:

Line 53: using (RSACryptoServiceProvider rsaProviderDecrypt = (RSACryptoServiceProvider)cerDecrypt.PublicKey.Key) Line 54:
{ Line 55: plainHashBytes = rsaProviderDecrypt.Decrypt(encryptedHashBytes, false); Line 56:
rsaProviderDecrypt.Clear(); Line 57:
rsaProviderDecrypt.Dispose();

Source File: E:\PayUSite\PayMvcApp\Controllers\HashMessageController.cs Line: 55

Stack Trace:

[CryptographicException: keyset does not exist. ]
System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +41
System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey) +0
System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP) +579

source code:

string docFile = Server.MapPath("~/docx/DirectAccess_StepByStep.doc");
HashAlgorithm hash = HashAlgorithm.Create("SHA1");
byte[] hashedBytes;
using (FileStream fs = new FileStream(docFile, FileMode.Open))
{
    //compute message hash value
    hashedBytes = hash.ComputeHash(fs);
    hash.Dispose();
    fs.Close();
}

string hashedString = Convert.ToBase64String(hashedBytes);

//encrypt message digest
string priKeyFile = Server.MapPath("~/certificate/WosMiddle.pfx");
X509Certificate2 certEncrypt = new X509Certificate2(priKeyFile, "123456");
byte[] encryptedHashBytes;
using (RSACryptoServiceProvider rsaProviderEncrypt = (RSACryptoServiceProvider)certEncrypt.PrivateKey)
{
    encryptedHashBytes = rsaProviderEncrypt.Encrypt(hashedBytes, false);
    rsaProviderEncrypt.Dispose();
}

//decrypt message digest
string pubKeyFile = Server.MapPath("~/certificate/WosMiddle-pubkey.cer");
X509Certificate2 cerDecrypt = new X509Certificate2(pubKeyFile);
byte[] plainHashBytes;
using (RSACryptoServiceProvider rsaProviderDecrypt = (RSACryptoServiceProvider)cerDecrypt.PublicKey.Key)
{
    //***will throw error message here...***
    plainHashBytes = rsaProviderDecrypt.Decrypt(encryptedHashBytes, false);
    rsaProviderDecrypt.Dispose();
}

//verify message whether was modified
string docFile2 = Server.MapPath("~/docx/DirectAccess_StepByStep.doc");
HashAlgorithm hash2 = HashAlgorithm.Create("SHA1");
byte[] hashedBytes2;
using (FileStream fs2 = new FileStream(docFile2, FileMode.Open))
{
    //compute message hash value
    hashedBytes2 = hash.ComputeHash(fs2);
    fs2.Close();
}

//compare hash value
bool isEqual = plainHashBytes.SequenceEqual(hashedBytes2);

解决方案

This question is old but for someone looking for the solution while continuing to use Encrypt and Decrypt here is how I manage to solve this error:

The base is my certificate have been installed the wrong way by double-clicking the .pfx file and selecting the store.

The wrong way to install the certificate

1. Double click the certificate:

2. The wizard opens, click in the next button:

3. The wizard show the certificates location, click in the next button:

4. Enter the password then click next:

5. Select the store then click next:

6. The wizard show the certificate information, click in Finish button

7. Succes dialog is showed:

So at this point I had the error "Keyset does not exist".


To solve that I proceeded this way (the correct way)

1. Execute Microsoft Management Console (mmc.exe):

2. A blank MMC instance showed:

3. Click in File->Add/Remove Snap-in...

4. Select certificate snap-in an click in Add button:

5. Select Computer account then click in Next button:

6. Select Local computer then click in Finish button:

7. The certificate snap-in is now added, click in OK button:

8. Select the personal store, then right click and select Import:

9. Browse the certificate, and click next:

10. Enter the password, then click in Next button:

11. Automatically select the certificate store:

12. The certificate information shows:

13. Success dialog message shows:

14. Refresh the MMConsole to show the certificate:

15. Right click on the certificate, then click in Manage Private Keys...:

16. Add the pool identity or the IIS user in my case I added IIS_IUSRS:

17. The user has been added, click on OK button:

And it is finished the keyset does exist now!!

这篇关于System.Security.Cryptography.CryptographicException:键集不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆