Working with Base64

Base64 is a transport encoding that is used to represent binary data, and therefore, any (relatively simple) data type.

Base64 is particularly useful when storing complex strings in files, or in text-based transport protocols, such as SMTP.

The .NET System.Convert class contains the following static methods that can be used to work with Base64:

  • ToBase64String
  • FromBase64String

The ToBase64String method takes an array of bytes and converts it into a string. For example, a simple byte array may be converted as follows:

PS> [Byte[]]$bytes = 97, 98, 99, 100, 101
PS> [Convert]::ToBase64String($bytes)
YWJjZGU=

A more meaningful byte sequence can be made from a few words by getting the byte values for each character:

PS> $bytes = [System.Text.Encoding]::ASCII.GetBytes('Hello world')
PS> [Convert]::ToBase64String($bytes)
SGVsbG8gd29ybGQ=

If the encoding is ASCII, it is possible in PowerShell to supply the ToBase64String method with an array of characters. Consider the following example:

PS> [Convert]::ToBase64String('Hello world'.ToCharArray())
SGVsbG8gd29ybGQ=

The text encoding type used here is ASCII (1 byte per character); UTF16 text encoding will result in a longer Base64 string, as each character is stored in two bytes:

PS> $bytes = [System.Text.Encoding]::Unicode.GetBytes('Hello world')
PS> [Convert]::ToBase64String($bytes)
SABlAGwAbABvACAAdwBvAHIAbABkAA==
Unicode encoding is used to create an encoded command

PowerShell.exe and pwsh.exe both have an EncodedCommand parameter. This can be any encoded script. The text must be Unicode-encoded.

Converting from a Base64 string into a sequence of bytes, and then into a string, may be achieved as follows:

PS> $base64String = 'YWJjZGU='
PS> $bytes = [Convert]::FromBase64String($base64String)
PS> [System.Text.Encoding]::ASCII.GetString($bytes)
abcde

Base64 can be a handy format for storing items such as keys (normally a set of bytes) for use with the ConvertTo-SecureString command. Consider the following example:

# Create a 16-byte key 
[Byte[]]$key = 1..16 | ForEach-Object { Get-Random -Minimum 0 -Maximum 256 }
# Convert the key to a string and save it in a file [Convert]::ToBase64String($key) | Out-File 'KeepThisSafe.txt' # Create a secure string (from plain text) to encrypt $secure = ConvertTo-SecureString -String 'Secure text' -AsPlainText -Force
# Encrypt the password using the key (from the file)
$convertFromSecureString = @{
SecureString = $secure
Key = [Convert]::FromBase64String((Get-Content .KeepThisSafe.txt))
}
$encrypted = ConvertFrom-SecureString @convertFromSecureString

# Decrypt the password using the same key
$convertToSecureString = @{
String = $encrypted
Key = [Convert]::FromBase64String((Get-Content .KeepThisSafe.txt))
}
$secure = ConvertTo-SecureString @convertToSecureString

# Show the original password
[PSCredential]::new('.', $secure).GetNetworkCredential().Password
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.199.243