2015-05-29 113 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'