System.Security.Cryptography.CryptographicException:keyset不存在
当我将x509证书发送到encypt和decypt消息时,我收到了一些错误信息,无法解决此问题。 有人会发生什么事来解决这个错误吗? 谢谢。
描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
例外细节:
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:键集不存在。 ]
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);
这个问题很老但是对于那些在继续使用Encrypt
and Decrypt
同时寻找解决方案的人来说,我是如何设法解决这个错误的:
通过双击.pfx
文件并选择商店,基础是我的证书安装错误。
安装证书的错误方法
1.双击证书:
2.向导打开,单击下一步按钮:
3.向导显示证书位置,单击下一步按钮:
4.输入密码,然后单击下一步:
5.选择商店,然后单击下一步:
6.向导显示证书信息,单击“ 完成”按钮
7.显示Succes对话框:
所以在这一点上我有错误“键集不存在” 。
为了解决这个问题,我采取了这种方式(正确的方式)
1.执行Microsoft管理控制台(mmc.exe):
一个空白的MMC实例显示:
3.单击文件 – >添加/删除管理单元…
4.在“ 添加”按钮中单击选择证书管理单元:
5.选择计算机帐户,然后单击下一步按钮:
6.选择本地计算机,然后单击完成按钮:
7.现在添加证书管理单元,单击确定按钮:
8.选择个人存储,然后右键单击并选择导入 :
9.浏览证书,然后单击下一步:
10.输入密码,然后单击“ 下一步”按钮:
11.自动选择证书存储:
12.证书信息显示:
13.成功对话框消息显示:
14.刷新MMConsole以显示证书:
15.右键单击证书,然后单击Manage Private Keys … :
16.在我添加IIS_IUSRS的情况下添加池标识或IIS用户:
17.添加了用户,单击确定按钮:
它完成了键盘确实存在 !!
应用程序可能正在尝试写入以下文件夹路径:C:\ Documents and Settings \ All Users \ Application Data \ Microsoft \ Crypto \ RSA \ MachineKeys
如果您的应用程序正在使用模拟或使用IUSR_MACHINENAME用户,则配置MachineKeys文件夹sercurities并为用户提供“读取和执行”,“列出文件夹内容”,“读取”,“写入”。 如果这不起作用,请尝试为Everyone用户授予相同的权限。
我相信在使用Encrypt和Decrypt时,它需要一个用于加密的公钥和一个用于解密的私钥。 所以它失败了,因为你试图在没有私钥的情况下进行解密并导致exception。
您应该使用SignData方法创建签名,并使用VerifyData进行validation。
当我在尝试签署SAML响应时未将我的证书中的PrivateKey加载到我的signedXmlElement的SigningKey中时遇到此错误。
signedElement.SigningKey = myCertificate.PrivateKey;
我收到与OP相同的错误:“System.Security.Cryptography.CryptographicException:keyset不存在”
决议(对我来说)是:Visual Studio需要(以管理员身份运行)
正如向我解释的那样(YMMV),VS需要作为Admin运行才能从密钥库中提取证书私钥,以便与keyvault协商auth / handshake。