我如何...使用x509certificate2获取私钥导致cryptographicexception“指定了无效的提供程序类型” [英] How do i...using x509certificate2 to get privatekey causes cryptographicexception “invalid provider type specified”
问题描述
大家,
我正在开发一个使用X509Certificate2从认证文件中获取私钥的Web应用程序。代码段如下所示:
everyone,
I am developing a web application that uses X509Certificate2 to get a private key from a certification file. Code snippet looks like following:
public static RSACryptoServiceProvider GetSignProviderFromPfx()
{
var strFileName = "c:\cer\mycerfile.pfx";
var strPassword = "000000";
X509Certificate2 pc = new X509Certificate2(strFileName, strPassword, X509KeyStorageFlags.MachineKeySet);
var ThePivateKey = pc.PrivateKey;
return (RSACryptoServiceProvider)ThePivateKey;
}
但是pc.Privatekey语句导致System.Security.Cryptography.CryptographicException指定了无效的提供程序类型。我确定认证文件没有问题,它确实有一个私钥。并且属性pc.HasPrivateKey也返回true。
测试环境是VS2013,窗口7.
有人可以提供帮助吗?
谢谢。
我尝试了什么:
我也试过以下:
a。我用iis express在VS2013中调试了它,出现了问题。
b。我在另一台与我的环境相同的计算机上进行了调试,问题也出现了。
c。我将应用程序发布到运行在Windows Web Server 2008 R2上的iis的服务器上,它工作正常。
d。我向widows azure网站发布了该应用程序,它也运行良好。
因此,我想代码片段没有问题。引发异常的关键原因是运行环境可能存在一些问题。我检查并比较了不同环境下认证文件的读/写权限,所有这些都是相同的。
But the statement pc.Privatekey causes a System.Security.Cryptography.CryptographicException "Invalid provider type specified" . I'm sure the certification file has no problem, it really has a private key. And the property pc.HasPrivateKey is also return true.
The test environment is VS2013, window 7.
Anybody can help?
Thanks.
What I have tried:
I also tried following:
a. I debugged it in VS2013 with iis express, the problem occured.
b. I debugged it in another computer with same enviroment with mine, the problem occured too.
c. I published the application to a server with iis running on Windows Web Server 2008 R2, it worked fine.
d. I published the application to widows azure website, it also worked fine.
Therefore, I guess the code snippet has no problem. The key reason raising the exception is that there may be some problem about running environment. I checked and compared the reading/writing right on the certification file in different environment, all of them are same.
推荐答案
检查这个 - 指定的提供者类型无效在CNG证书上访问X509Certificate2.PrivateKey时出错 - 解密我的世界 [ ^ ]。
Check this - "Invalid provider type specified" error when accessing X509Certificate2.PrivateKey on CNG certificates – Decrypt my World[^].
这篇关于我如何...使用x509certificate2获取私钥导致cryptographicexception“指定了无效的提供程序类型”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!