Powershell获取新自签名证书的base64公钥

问题描述 投票:0回答:1

我知道如何在 powershell 中创建新的自签名证书,但我想获取该新创建的证书的 base64 公钥,例如用于导出到需要 Base64 编码 DER 的服务。

例如:

$dateformatted = (Get-Date).ToString('yyyy-MM-dd')
$certname = "the-cert-name-$dateformatted"
$cert = New-SelfSignedCertificate -Subject "CN=$certname" -CertStoreLocation cert:\LocalMachine\My

如何获取base64格式的

$cert
公钥?

powershell certificate
1个回答
0
投票

Export-Certificate
powershell 命令为您提供公钥的二进制版本,然后您可以使用标准 .Net 类读取这些字节并将它们再次以 base64 写回。

$dateformatted = (Get-Date).ToString('yyyy-MM-dd')
$certname = "the-cert-name-$dateformatted"
$cert = New-SelfSignedCertificate -Subject "CN=$certname" -CertStoreLocation cert:\LocalMachine\My
$binaryFile = New-TemporaryFile
$exported = Export-Certificate -Cert $cert -FilePath $binaryFile
$binaryContent = [System.IO.File]::ReadAllBytes("$binaryFile")
[System.Convert]::ToBase64String($binaryContent) | Out-File "$certname.cer"
Write-Output "Wrote $certname.cer"
remove-item $binaryFile
© www.soinside.com 2019 - 2024. All rights reserved.