繁体   English   中英

System.Security.Cryptography.CryptographicException:密钥集不存在

[英]System.Security.Cryptography.CryptographicException: keyset does not exist

当我制作一个x509证书来加密和解密消息时,我得到了一些错误信息并且无法解决这个问题。 有人能碰巧解决这个错误吗? 谢谢。

说明:在执行当前 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,Byte [] pbEncryptedKey,Int32 cbEncryptedKey,Boolean fOAEP,ObjectHandleOnStack ohRetDecryptedKey)+0
System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP) +579

源代码:

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);

这个问题很老,但对于在继续使用EncryptDecrypt同时寻找解决方案的人来说,这是我设法解决此错误的方法:

基础是我的证书已通过双击.pfx文件并选择商店以错误的方式安装。

安装证书的错误方法

1.双击证书:

证书文件

2.向导打开,点击下一步按钮:

向导 0

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

向导 1

4.输入密码然后点击下一步:

向导 2

5.选择商店,然后单击下一步:

巫师 3

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

向导 4

7.显示成功对话框:

巫师 5

所以在这一点上我有错误"Keyset does not exist"


为了解决这个问题,我是这样做的(正确的方法)

1.执行微软管理控制台(mmc.exe):

执行mmc

2.一个空白的MMC实例显示:

mmc 显示

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

添加管理单元

4.选择证书管理单元并单击添加按钮:

添加证书管理单元

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

选择电脑账户

6.选择本地计算机,然后单击完成按钮:

选择本地计算机

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

证书管理单元显示

8.选择个人商店,然后右键单击并选择导入

选择个人商店并导入

9.浏览证书,点击下一步:

浏览证书

10.输入密码,然后点击下一步按钮:

在此处输入图片说明

11.自动选择证书库:

自动选择商店

12 、证书信息显示:

证书信息

13.成功对话框信息显示:

在此处输入图片说明

14.刷新 MMConsole 以显示证书:

刷新mmc

15.右键单击证书,然后单击Manage Private Keys...

管理私钥

16.添加池标识或 IIS 用户,在我添加 IIS_IUSRS 的情况下:

添加 iis_iusrs

17.用户已添加,点击OK按钮:

用户添加

它完成了键组现在确实存在!!

我收到与 OP 相同的错误:“System.Security.Cryptography.CryptographicException:keyset does not exist”

解决方案(对我而言)是:Visual Studio 需要(以管理员身份运行)

正如向我解释的那样(YMMV),VS 需要以管理员身份运行才能从密钥库中提取证书私钥,以便与密钥库协商身份验证/握手。

应用程序可能正在尝试写入以下文件夹路径:C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Crypto\\RSA\\MachineKeys

如果您的应用程序使用模拟或使用 IUSR_MACHINENAME 用户,则配置 MachineKeys 文件夹 sercurities 并为用户提供读取和执行、列出文件夹内容、读取、写入。 如果这不起作用,请尝试为 Everyone 用户授予相同的权限。

我确实遇到了同样的问题。 该消息并不理想,在我的情况下,我的用户无权访问私钥。 您可以使用以下步骤解决此问题:

  1. 打开 mmc
  2. 添加证书管理单元
  3. 选择您要使用的证书
  4. 右键单击它并选择“所有任务”/“管理私钥...”
  5. 将您的用户添加到授权用户列表并允许“完全控制”

尝试以管理员身份运行 vs。 为我工作

我相信在使用 Encrypt 和 Decrypt 时,它需要一个用于加密的公钥和一个用于解密的私钥。 所以它失败了,因为你试图在没有私钥的情况下解密,这导致了异常。

您确实应该使用SignData方法来创建签名并使用VerifyData进行验证。

我在尝试签署 SAML 响应时没有将 PrivateKey 从我的证书加载到我的 signedXmlElement 的 SigningKey 中时遇到了这个错误。

signedElement.SigningKey = myCertificate.PrivateKey;

.Net 在此处创建临时证书文件“..\Microsoft\Crypto\RSA\MachineKeys”。 可能是证书文件仍然锁定了其他进程。 尝试重新启动应用程序然后电脑。 如果问题解决了,您需要创建 manuelly.pfx 文件,然后在您的电脑上加载证书应用程序。

在我的情况下,私钥存储在"C:\\ProgramData\\Microsoft\\Crypto\\Keys"而不是 machinekeys 文件夹中 - 您可以使用 certutil 检查以找出将成为私钥的“唯一容器名称”。

我现在扫描加密目录以找到匹配项。 通过匹配,我可以在适当的文件上设置正确的 ACL

如果您在从 visual studio 运行代码时遇到此问题,那么以管理员身份运行 visual studio 将解决问题,或者如果您在 IIS 上发布后遇到此问题,则以下步骤将解决问题:

1. 打开 IIS 并右键单击 API/网站并单击管理应用程序 => 高级设置以查找正在使用的应用程序池

在此处输入图像描述

在此处输入图像描述

2.点击应用池

在此处输入图像描述

3.右击你API使用的Application Pool然后select“Advanced Settings”

在此处输入图像描述

4.将“身份”更改为“本地系统”,然后按“确定”按钮

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM