我試圖使用最初導入到使用MMC的「證書」管理單元的Windows 10計算機上的CurrentUser密鑰庫的X509證書。已經在Windows 8.1計算機上測試過相同的過程,結果相同。.NET Framework x509Certificate2類,HasPrivateKey == true && PrivateKey == null?
使用PowerShell的標準PKI模塊,我使用的是得到一個X509Certificate2對象獲取-項目:
$my_cert = Get-Item Cert:\CurrentUser\My\ADAA82188A17THUMBPRINTXXXXXXXXXXX
的$my_cert | fl *
輸出如下:
PSPath : Microsoft.PowerShell.Security\Certificate::CurrentUser\My\XXXXXXXXXXXXXXXXXXX
PSParentPath : Microsoft.PowerShell.Security\Certificate::CurrentUser\My
PSChildName : XXXXXXXXXXXXXXXXXXX
PSDrive : Cert
PSProvider : Microsoft.PowerShell.Security\Certificate
PSIsContainer : False
EnhancedKeyUsageList : {Secure Email (1.3.6.1.5.5.7.3.4), IP security user (1.3.6.1.5.5.7.3.7), Encrypting File
System (1.3.6.1.4.1.311.10.3.4), Document Signing (1.3.6.1.4.1.311.10.3.12)...}
DnsNameList : {My Name}
SendAsTrustedIssuer : False
EnrollmentPolicyEndPoint : Microsoft.CertificateServices.Commands.EnrollmentEndPointProperty
EnrollmentServerEndPoint : Microsoft.CertificateServices.Commands.EnrollmentEndPointProperty
PolicyId : {D52C406F-C279-4BF2-B7C2-EE704290DB3E}
Archived : False
Extensions : {System.Security.Cryptography.Oid, System.Security.Cryptography.Oid,
System.Security.Cryptography.Oid, System.Security.Cryptography.Oid...}
FriendlyName :
IssuerName : System.Security.Cryptography.X509Certificates.X500DistinguishedName
NotAfter : 4/15/2017 2:15:16 PM
NotBefore : 4/15/2016 2:15:16 PM
HasPrivateKey : True
PrivateKey :
PublicKey : System.Security.Cryptography.X509Certificates.PublicKey
RawData : {56, 130, 19, 252...}
SerialNumber : 4F0000002F700000000000000000000000
SubjectName : System.Security.Cryptography.X509Certificates.X500DistinguishedName
SignatureAlgorithm : System.Security.Cryptography.Oid
Thumbprint : XXXXXXXXXXXXXXXXXXX
Version : 3
Handle : 2241663016272
Issuer : CN=Issuing CA, DC=My, DC=Domain, DC=us
Subject : [email protected], CN=My Name
所以HasPrivateKey ==真,但PrivateKey == null。我一直在想如何獲得訪問私鑰來執行加密和解密。我在網上看到的所有例子都似乎表明X509Certificate2類的PrivateKey屬性應該是可用的,但顯然我錯過了一些東西。
我讀過類似的問題,例如Empty PrivateKey in x509certificate2,但沒有一個能夠解決我的問題。我也讀過Paul Stovell的Eight tips for working with X.509 certificates in .NET,這很有啓發,但最終沒有幫助。這也幫助我確認私鑰則在正確的位置,而且據我所知,有正確的權限由x509Certificate2類引用:
C:\Users\My.Name\AppData\Roaming\Microsoft\SystemCertificates\My\Keys
密鑰文件的名稱相匹配證書上的主題密鑰標識符。
編輯:
的certutil -user -store my "Serial Number"
輸出是:
Serial Number: 4f000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Issuer: CN=Issuing CA, DC=My, DC=Domain, DC=us
NotBefore: 4/15/2016 2:15 PM
NotAfter: 4/15/2017 2:15 PM
Subject: [email protected], CN=My Name
Non-root Certificate
Template: Userv1, User v1
Cert Hash(sha1): ad ab 82 18 8a 17 4d 75 11 04 48 7c 43 43 d4 05 b9 74 c8 4c
Key Container = te-Userv1-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Unique container name: fcbba1aa0xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Provider = Microsoft Software Key Storage Provider
Encryption test passed
CertUtil: -store command completed successfully.
什麼 「鑰匙」 的資料片我錯過了?爲什麼不能方便地從X509Certificate2對象中引用私鑰?我如何獲得訪問權限?
嘗試'$ my_cert.get_PrivateKey()'。請問它會拋出任何異常? – PetSerAl
@PetSerAl,我確實得到了一個異常 - 「無效的提供程序類型指定。」 –