Aleksandar Ivanov
Aleksandar Ivanov

Reputation: 317

PFX encoded and back to PFX in Powershell

Is it possible when you convert a PFX to lets say Base64, to then convert it back to 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'

The output of line four is unsurprisingly invalid, but I am not sure how to go about it.

Upvotes: 2

Views: 3563

Answers (1)

Clint Oliveira
Clint Oliveira

Reputation: 702

I created a PFX cert in location : C:\temp\PowerShellGraphCert.pfx and ran the following. I believe this is what you are looking for. I converted > PowerShellGraphCert.pfx to PowerShellGraphCert.txt and then back to dummy-3.pfx. Now 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

Upvotes: 7

Related Questions