我知道如何在 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
公钥?
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