System.Security.Cryptography.CryptographicException:keyset不存在

Bes*_*Ley 39 c# x509certificate

当我将x509证书发送到encypt和decypt消息时,我收到了一些错误信息,无法解决此问题.有人会发生什么事来解决这个错误吗?谢谢.

描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

例外细节:

System.Security.Cryptography.CryptographicException:keyset不存在.

来源错误:

第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:keyset不存在.]
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

源代码:

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);
Run Code Online (Sandbox Code Playgroud)

sab*_*ero 102

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

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

安装证书的错误方法

1.双击证书:

证书文件

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

向导0

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

向导1

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

向导2

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

巫师3

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

巫师4

7.显示Succes对话框:

巫师5

所以在这一点上我有错误"键集不存在".


为了解决这个问题,我采取了这种方式(正确的方式)

1.执行Microsoft管理控制台(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_IUSRS的情况下添加池标识或IIS用户:

添加iis_iusrs

17.添加了用户,单击确定按钮:

用户添加

它完成了现在存在的键集!

  • 您可以通过说"用户没有私钥权限,因此您需要授予权限"来简化您的答案.解决方案与导入证书的方式无关(没有"正确"或"错误"的导入方式).感谢您解决方案的最后一步! (15认同)
  • 我也有这个问题,设置证书权限正是问题所在!谢谢你的出色答案; 截图ftw! (7认同)
  • 就我而言,从MMC控制台安装证书是有效的.双击.PFX安装没有. (3认同)
  • 第 15 和 16 行项目很重要。 (3认同)
  • 这通常是权限问题。您需要向运行应用程序/服务的用户帐户授予读取权限。 (2认同)

小智 8

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

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

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


Joh*_*ber 8

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

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


eMi*_*eMi 6

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

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


Bha*_*esh 6

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