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.向导打开,单击下一步按钮:
3.向导显示证书位置,单击下一步按钮:
4.输入密码,然后单击下一步:
5.选择商店,然后单击下一步:
6.向导显示证书信息,单击" 完成"按钮
7.显示Succes对话框:
1.执行Microsoft管理控制台(mmc.exe):
2.一个空白MMC实例表明:
3.单击文件 - >添加/删除管理单元...
4.在" 添加"按钮中单击选择证书管理单元:
5.选择计算机帐户,然后单击下一步按钮:
6.选择本地计算机,然后单击完成按钮:
7.现在添加证书管理单元,单击确定按钮:
8.选择个人存储,然后右键单击并选择导入:
9.浏览证书,然后单击"下一步":
10.输入密码,然后单击" 下一步"按钮:
11.自动选择证书存储:
12.证书信息显示:
13.成功对话框消息显示:
14.刷新MMConsole以显示证书:
15.右键单击证书,然后单击Manage Private Keys ...:
16.在我添加IIS_IUSRS的情况下添加池标识或IIS用户:
17.添加了用户,单击确定按钮:
小智 8
我收到与 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文件夹的安全性,并为用户提供“读取和执行”,“列出文件夹内容”,“读取”,“写入”。如果这不起作用,请尝试为Everyone用户赋予相同的权限。
归档时间: |
|
查看次数: |
66129 次 |
最近记录: |