gpt4 book ai didi

c# - 系统.Security.Cryptography.CryptographicException : keyset does not exist

转载 作者:IT王子 更新时间:2023-10-29 04:00:38 25 4
gpt4 key购买 nike

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

描述:当前网络请求执行过程中出现未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详情:

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

Source Error:

Line 53: using (RSACryptoServiceProviderrsaProviderDecrypt =(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(Int32hr) +41
System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandlepKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, BooleanfOAEP, 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. 双击证书:

certificate file

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

wizard 0

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

wizard 1

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

wizard 2

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

wizard 3

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

wizard 4

7. 显示成功对话框:

wizard 5

所以此时我遇到了错误“Keyset does not exist”


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

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

execute mmc

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

mmc showed

3. 单击"file"->“添加/删除管理单元...”

add snap-in

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

add certificate snap-in

5. 选择Computer account 然后点击Next 按钮:

select computer account

6. 选择Local computer 然后点击Finish 按钮:

selecct local computer

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

certificate snap-in shows

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

select personal store and import

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

browse certificate

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

enter image description here

11. 自动选择证书存储:

automatically select the store

12.证书信息显示:

certificate information

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

enter image description here

14. 刷新 MMConsole 以显示证书:

refresh mmc

15. 右键单击​​证书,然后单击管理私钥...:

manage private keys

16. 在我添加 IIS_IUSRS 的情况下添加池身份或 IIS 用户:

add iis_iusrs

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

user added

完成了键集现在存在!!

关于c# - 系统.Security.Cryptography.CryptographicException : keyset does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12106011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com