无法读取X509Certificate2上的私钥

5

操作系统:Win 8.1 64位。.NET版本:4.5。Powershell版本:4.0。(尽管我认为这个问题与.NET有关,但并不特定于Powershell。)

我通过以下方式生成了一个自签名证书:

New-SelfSignedCertificate -DnsName 'TheIncredibleHulk' -CertStoreLocation Cert:\CurrentUser\My

然后我取回证书:

$Cert = Get-Item Cert:\CurrentUser\My\9E5A8322B890DA1247BD98BDAB288CA9D11CF99E

证书有一个私钥:
$Cert.HasPrivateKey
True

或者它确实有效吗?
$Cert.PrivateKey
(null)

为什么PrivateKey成员为空?我该如何检索私钥?

我猜测这是使用PSPKI(https://pspki.codeplex.com/)吗? - Kev
@Kev 不,没有使用自定义模块。 - Ryan Ries
必须向执行访问操作的用户授予私钥访问权限。在certmgr.msc中,右键单击证书,选择“所有任务”->“管理私钥”,并授予访问权限。 - John Saunders
谢谢@JohnSaunders,但我不认为权限是问题,因为这个证书已经在我的用户帐户的个人存储中,并且我使用生成该证书的相同用户帐户访问它。 - Ryan Ries
2个回答

6

6

这个答案 帮我找到了解决办法。问题是由于默认提供程序引起的,但在 Win10版本的New-SelfSignedCertificate 中,可以按照下面所示指定提供程序。

New-SelfSignedCertificate -DnsName HulkSmash -CertStoreLocation Cert:\LocalMachine\My  -KeyAlgorithm RSA -KeyLength 2048 -KeyExportPolicy Exportable -KeyProtection None -Provider 'Microsoft Enhanced RSA and AES Cryptographic Provider'

太好了!非常感谢您的回答! - Roberto Bonini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接