Ogg*_*las 18 windows powershell certificate certificate-store
正如标题所示,我想在不使用 OpenSSL 或任何其他第三方工具的情况下导出我的私钥。如果我需要一个.cer
文件或.pfx
文件,我可以通过 MMC 或 PowerShell 轻松导出这些文件,pkiclient
但我找不到获取私钥的方法。
https://docs.microsoft.com/en-us/powershell/module/pkiclient/export-certificate?view=win10-ps
使用像https://www.sslshopper.com/ssl-converter.html这样的在线工具是不行的。
PS版本:
PS C:\Users\oscar> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17134.228
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17134.228
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Run Code Online (Sandbox Code Playgroud)
我可以像这样获得公钥:
(Get-PfxCertificate -FilePath C:\Users\oscar\Desktop\localhost.pfx).GetPublicKey()
Run Code Online (Sandbox Code Playgroud)
并像这样导出整个证书:
(Get-PfxCertificate -FilePath C:\Users\oscar\Desktop\localhost.pfx).GetRawCertData()
Run Code Online (Sandbox Code Playgroud)
结果来自
PS C:\Users\oscar> $mypwd = ConvertTo-SecureString -String "MyPassword" -Force -AsPlainText
PS C:\Users\oscar> $mypfx = Get-PfxData -FilePath C:\Users\oscar\Desktop\localhost.pfx -Password $mypwd
PS C:\Users\oscar> $mypfx
OtherCertificates EndEntityCertificates
----------------- ---------------------
{} {[Subject]...
PS C:\Users\oscar> $mypfx.EndEntityCertificates
Thumbprint Subject
---------- -------
8ED4971564E35099D6DB490C3756E2AD43AAAAAA CN=localhost
Run Code Online (Sandbox Code Playgroud)
测试了来自@Brad 的命令,但出现以下错误。
私钥不能纯文本导出
certutil -exportPFX -p "myPassword" -privatekey -user my <Certificate Serial Number> C:\localhost.pfx
Run Code Online (Sandbox Code Playgroud)
与 MMC 证书中的证书导出向导类似,仅.pfx
当包含密钥时才导出为可用。
我遇到了同样的问题,并在 PS Gallery的PSPKI Powershell 模块的帮助下解决了它。虽然我知道您正在寻找最好使用 Windows 中某些内置功能的解决方案,但从 PS Gallery 安装模块可能是可以接受的。至少在我的情况下是这样。
首先安装 PSPKI 模块(我假设已经设置了 PSGallery 存储库):
Install-Module -Name PSPKI
Run Code Online (Sandbox Code Playgroud)
PSPKI 模块提供了一个 Cmdlet Convert-PfxToPem
,它将 pfx 文件转换为 pem 文件,其中包含作为 base64 编码文本的证书和盗版密钥:
Convert-PfxToPem -InputFile C:\path\to\pfx\file.pfx -Outputfile C:\path\to\pem\file.pem
Run Code Online (Sandbox Code Playgroud)
现在,我们需要做的就是使用一些正则表达式来拆分 pem 文件。例如,像这样:
(Get-Content C:\path\to\pem\file.pem -Raw) -match "(?ms)(\s*((?<privatekey>-----BEGIN PRIVATE KEY-----.*?-
----END PRIVATE KEY-----)|(?<certificate>-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----))\s*){2}"
$Matches["privatekey"] | Set-Content "C:\path\to\key\file.pem"
$Matches["certificate"] | Set-Content "C:\path\to\certificate\file.pem"
Run Code Online (Sandbox Code Playgroud)
我发现Panos.G 的答案很有希望,但没有让它发挥作用。所有描述的三种方法在我的证书对象上均不可用。经过更多挖掘,我提出了以下解决方案:
注意:如果您从证书存储中读取证书,则它可以工作。例如,如果您读取.pfx
带有 的文件,则它不起作用。Get-PfxCertificate
如果您只是将其作为文件,则可以将其安装在证书存储中,以便能够从那里读取它,如下所示。
# Read the certificate from the certificate store
# In this example, I use the certificate thumbprint to identify the certificate.
$cert = Get-ChildItem Cert:\ -Recurse | ? {$_.Thumbprint -eq '<THUMBPRINT_OF_CERTIFICATE>'}
# Read the private key into an RSA CNG object:
$RSACng = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($cert)
# Get the bytes of the private key
$KeyBytes = $RSACng.Key.Export([System.Security.Cryptography.CngKeyBlobFormat]::Pkcs8PrivateBlob)
# Encode the bytes (Base64)
$KeyBase64 = [System.Convert]::ToBase64String($KeyBytes, [System.Base64FormattingOptions]::InsertLineBreaks)
# Put it all together
$KeyPem = @"
-----BEGIN PRIVATE KEY-----
$KeyBase64
-----END PRIVATE KEY-----
"@
Run Code Online (Sandbox Code Playgroud)
文件:
归档时间: |
|
查看次数: |
17030 次 |
最近记录: |