String Encoding and Decoding - Base64

Written on April 8, 2017

Here’s a quick one. In this post we’ll create 2 basic functions for encoding and decoding strings.

We will need to use some .net classes for this. Here’s the classes for encoding:

  1. [System.Text.Encoding]
  2. [System.Convert]

Since [System] is always loaded, it will be omitted.

Encoding Strings


$encodeMe='Gibberish'
$uniBytes=[Text.Encoding]::Unicode.GetBytes($encodeMe)
$encoded=[Convert]::ToBase64String($uniBytes)
Write-Output "Now it really is gibberish -> $encoded"

The same process can be used using different encodings; ASCII for example.

$encodeMe='Gibberish'
$asciiBytes=[Text.Encoding]::ASCII.GetBytes($encodeMe)
$encoded=[Convert]::ToBase64String($asciiBytes)
Write-Output "Now it's different gibberish -> $encoded"

Pretty straightforward stuff:

  1. Define your string
  2. Get the byte array of the string
  3. Convert to Base64

Decoding Strings


As you may expect, decoding is essentially the same process, just in reverse.

# Output from first example
$decodeMe='RwBpAGIAYgBlAHIAaQBzAGgA'
$baseBytes=[Convert]::FromBase64String($decodeMe)
$decoded=[Text.Encoding]::Unicode.GetString($baseBytes)
Write-Output "No longer -> $decoded"

And if you wanted to use ASCII:

# Output from second example
$decodeMe='R2liYmVyaXNo'
$baseBytes=[Convert]::FromBase64String($decodeMe)
$decoded=[Text.Encoding]::ASCII.GetString($baseBytes)
Write-Output "No longer -> $decoded"

Again, this is quite simple to follow:

  1. Define your encoded string
  2. Get byte array from that string - from Base64
  3. Decode the byte array

The Functions


Let’s make a couple functions for encoding and decoding strings.

Encode
function ConvertTo-EncodedString {
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory=$true,
            Position=0,
            ValueFromPipeline=$true
        )]
        [String]$String
    )
    Begin{}
    Process{
        [Byte[]]$uniBytes=[Text.Encoding]::Unicode.GetBytes($String)
        [String]$encode=[Convert]::ToBase64String($uniBytes)
        return $encode
    }
    End{}
}
Decode
function ConvertFrom-EncodedString {
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory=$true,
            Position=0,
            ValueFromPipeline=$true
        )]
        [String]$String
    )
    Begin{}
    Process{
        [Byte[]]$baseBytes=[Convert]::FromBase64String($String)
        [String]$decode=[Text.Encoding]::Unicode.GetString($baseBytes)
        return $decode
    }
    End{}
}

You could make this much more versatile by adding support for more encoding options like ASCII, but this is a start (I added this functionality here). We will also be able to use these tools for future projects.


Thanks for reading

PS> exit