PFX 编码并返回到 Powershell 中的 PFX

问题描述

是否可以将 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'

第 4 行的输出毫无疑问是无效的,但我不知道如何去做。

解决方法

我在以下位置创建了 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)

enter image description here