[英]An exception of type 'System.Security.Cryptography.CryptographicException': keyset does not exist
[英]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);
这个问题很老,但对于在继续使用Encrypt
和Decrypt
同时寻找解决方案的人来说,这是我设法解决此错误的方法:
基础是我的证书已通过双击.pfx
文件并选择商店以错误的方式安装。
1.双击证书:
2.向导打开,点击下一步按钮:
3.向导显示证书位置,单击下一步按钮:
4.输入密码然后点击下一步:
5.选择商店,然后单击下一步:
6.向导显示证书信息,点击完成按钮
7.显示成功对话框:
1.执行微软管理控制台(mmc.exe):
2.一个空白的MMC实例显示:
3.单击文件->添加/删除管理单元...
4.选择证书管理单元并单击添加按钮:
5.选择计算机帐户,然后单击下一步按钮:
6.选择本地计算机,然后单击完成按钮:
7.现在添加了证书管理单元,点击确定按钮:
8.选择个人商店,然后右键单击并选择导入:
9.浏览证书,点击下一步:
10.输入密码,然后点击下一步按钮:
11.自动选择证书库:
12 、证书信息显示:
13.成功对话框信息显示:
14.刷新 MMConsole 以显示证书:
15.右键单击证书,然后单击Manage Private Keys... :
16.添加池标识或 IIS 用户,在我添加 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 用户授予相同的权限。
我确实遇到了同样的问题。 该消息并不理想,在我的情况下,我的用户无权访问私钥。 您可以使用以下步骤解决此问题:
尝试以管理员身份运行 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.