2015-05-29 44 views
1

Win 8.1 64位。 .NET 4.5。 Powershell 4.0。 (虽然我觉得这个问题是关系到.NET一般和不特定于PowerShell的。)无法读取X509Certificate2上的私钥

我生成自签名证书是这样的:

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

然后我检索证书:

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

证书具有私有密钥:

$Cert.HasPrivateKey 
True 

还是没有呢?

$Cert.PrivateKey 
(null) 

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

+0

我推测这是使用PSPKI(https://pspki.codeplex.com/)? – Kev

+0

@Kev不,没有使用自定义模块。 –

+0

有必要授予访问权限的用户对私钥的访问权限。在certmgr.msc中,右键单击证书,执行“所有任务” - >管理私钥并授予访问权限。 –

回答

3

对于其他发生此问题的人,我怀疑此问题与New-SelfSignedCertificate使用的存储提供程序有关(请参阅http://blogs.technet.com/b/vishalagarwal/archive/2010/03/30/verifying-the-private-key-property-for-a-certificate-in-the-store.aspx,其中讨论了Microsoft软件密钥存储提供程序和.NET类的问题)。 technet上有一个可用于创建自签名证书的powershell脚本,并且默认使用不同的存储提供程序。

1

This answer使我找到了解决方法。该问题是由默认提供程序引起的,但在Win10 version 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'