PowerShell – Base64 Encode/Decode

email me

This is how you embed resource files right into your PowerShell script, meaning….if you have loose files that are required by your script, you can include those files inside your script. Very cool!

Encoding/Decoding a Text File

clear-host

# Encode

$File1 = "c:\setup\YourFile.txt"

$Content1 = get-content $File1
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($Content1)
$Encoded = [System.Convert]::ToBase64String($Bytes)
#$Encoded | set-content ($File1 + ".b64")
Write-Host "ENCODED: " $Encoded

# Decode
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($Encoded)) | Out-File -Encoding "ASCII" c:\setup\YourFileDecoded.txt
$Content2 = get-content c:\setup\YourFileDecoded.txt
Write-Host "DECODED: " $Content2

 

Encoding/Decoding an EXE

clear-host

# Encode

$FilePath = "c:\setup\foo.exe"
$File = [System.IO.File]::ReadAllBytes($FilePath);

# returns the base64 string
$Base64String = [System.Convert]::ToBase64String($File);

# Decode

function Convert-StringToBinary {
[CmdletBinding()]
param (
[string] $EncodedString
, [string] $FilePath = (‘{0}\{1}’ -f $env:TEMP, [System.Guid]::NewGuid().ToString())
)

try {
if ($EncodedString.Length -ge 1) {

# decodes the base64 string
$ByteArray = [System.Convert]::FromBase64String($EncodedString);
[System.IO.File]::WriteAllBytes($FilePath, $ByteArray);
}
}
catch {
}

Write-Output -InputObject (Get-Item -Path $FilePath);
}

$DecodedFile = Convert-StringToBinary -EncodedString $Base64String -FilePath C:\setup\foo.exe

 


Notes

Decode Base64 Image String

$Base64String = 'TheBase64StringHere'
$Image = "$env:TEMP\ImageName.png"
[byte[]]$Bytes = [convert]::FromBase64String($Base64String)
[System.IO.File]::WriteAllBytes($Image,$Bytes)

 

Read File into Byte Array

[byte[]]$Bytes1 = Get-Content "C:\PowerShell\foo1.exe" -Encoding byte

 

Write Byte Array to File

[System.IO.File]::WriteAllBytes("C:\PowerShell\foo2.exe", $Bytes2)

 

Convert Hex String to Byte Array

$Bytes2 = [byte[]]::new($Hex.Length / 2)

For($i=0; $i -lt $Hex.Length; $i+=2){
$Bytes2[$i/2] = [convert]::ToByte($Hex.Substring($i, 2), 16)
}

 

Convert Byte Array to Hex

$Hex = [bitconverter]::ToString($Bytes1).Replace("-", "");

or

$Hex = [System.Text.StringBuilder]::new($Bytes.Length * 2)

ForEach($byte in $Bytes){
$Hex.AppendFormat("{0:x2}", $byte) | Out-Null
}

 

Return Hex Values

function Get-HexValues
{
[CmdletBinding()] Param
(
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
[Alias("FullName","FilePath")] $Path,
[Int] $Width = 16,
[Int] $Count = -1,
[String] $PlaceHolder = ".",
[Switch] $NoOffset,
[Switch] $NoText
)

$linecounter = 0
#$placeholder = "."
get-content $path -encoding byte -readcount $width -totalcount $count |
foreach-object `
{
$paddedhex = $text = $null
$bytes = $_
foreach ($byte in $bytes)`
{
$byteinhex = [String]::Format("{0:X}", $byte)
$paddedhex += $byteinhex.PadLeft(2,"0") + " "
}

if ($paddedhex.length -lt $width * 3)
{ $paddedhex = $paddedhex.PadRight($width * 3," ") }
foreach ($byte in $bytes)`
{
if ( [Char]::IsLetterOrDigit($byte) -or
[Char]::IsPunctuation($byte) -or
[Char]::IsSymbol($byte) )
{ $text += [Char] $byte }
else
{ $text += $placeholder }
}
$offsettext = [String]::Format("{0:X}", $linecounter)
$offsettext = $offsettext.PadLeft(8,"0") + "h:"
$linecounter += $width # Increment linecounter.
if (-not $NoOffset) { $paddedhex = "$offsettext $paddedhex" }
if (-not $NoText) { $paddedhex = $paddedhex + $text }
$paddedhex
}
}
Get-HexValues C:\PowerShell\foo1.exe

 

Online Base64

 

tags: MrNetTek