我正在尝试处理一个X509证书,该证书最初是使用MMC的“证书”插件导入到Windows 10计算机的CurrentUser密钥库中的。同样的过程已在Windows 8.1计算机上进行了测试并得到了相同的结果。
使用标准的PowerShell PKI模块,我正在使用Get-Item获取一个X509Certificate2对象:
所以HasPrivateKey == True,但PrivateKey == null。我一直在尝试找出如何访问私钥以执行加密和解密。我在网上看到的示例似乎都表明X509Certificate2类的PrivateKey属性应该是readily available,但显然我错过了什么。
我已经阅读了类似的问题,比如Empty PrivateKey in x509certificate2,但没有一个能解决我的问题。我也阅读了Paul Stovell的Eight tips for working with X.509 certificates in .NET,这非常有启发性,但最终没有帮助。它确实帮助我验证了私钥存在于正确的位置,并且据我所知,具有正确的权限可以被x509Certificate2类引用:
我在这里缺少什么“关键”信息?为什么私钥不能方便地从X509Certificate2对象中引用?我如何获得访问权限?
使用标准的PowerShell PKI模块,我正在使用Get-Item获取一个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 : E=my.name@my.domain.us, CN=My Name
所以HasPrivateKey == True,但PrivateKey == null。我一直在尝试找出如何访问私钥以执行加密和解密。我在网上看到的示例似乎都表明X509Certificate2类的PrivateKey属性应该是readily available,但显然我错过了什么。
我已经阅读了类似的问题,比如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: 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: E=my.name@my.domain.us, 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()
。它是否会抛出任何异常? - user4003407