PFX 编码并返回到 Powershell 中的 PFX

PFX encoded and back to PFX in Powershell

当您将 PFX 转换为 Base64,然后再将其转换回 PFX 时,是否可以?

$PFX_FILE = get-content 'dummy.pfx' -Encoding Byte
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($PFX_FILE)) | Out-File 'dummy.txt'
$BASE64_STR = get-content 'dummy.txt' -Encoding utf8
[Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($BASE64_STR)) | Out-File 'dummy-2.pfx'

第四行的输出毫无疑问是无效的,但我不确定该怎么做。

我在以下位置创建了一个 PFX 证书:C:\temp\PowerShellGraphCert.pfx 和 运行。我相信这就是您要找的。 我将 > PowerShellGraphCert.pfx 转换为 PowerShellGraphCert.txt,然后又转换回 dummy-3.pfx。 现在PowerShellGraphCert.pfx = dummy-3.pfx

$PFX_FILE = get-content 'C:\temp\PowerShellGraphCert.pfx' -Encoding Byte
$base64 = [System.Convert]::ToBase64String($PFX_FILE) | Out-File 'C:\temp\PowerShellGraphCertbase64.txt'

$BASE64_STR = get-content 'C:\temp\PowerShellGraphCertbase64.txt'
$filename = 'C:\temp\dummy-3.pfx'
$bytes = [Convert]::FromBase64String($BASE64_STR)
[IO.File]::WriteAllBytes($filename, $bytes)