Skip to content

Tag: Base64

Base64 Encoding with PowerShell

Base64 encoding is a method used to encode binary data into an ASCII string format by translating it into a radix-64 representation. It is commonly used in various applications, including encoding email attachments, JSON web tokens, and data storage. This method ensures that the data remains intact without modification during transport.

PowerShell includes built-in support for base64 encoding and decoding. In this guide, we’ll explore how to perform base64 encoding in PowerShell.

What is Base64 Encoding?

Base64 encoding converts binary data into a text format using 64 different ASCII characters. These characters include uppercase letters (A-Z), lowercase letters (a-z), numbers (0-9), and two additional symbols (+ and /). This encoding is particularly useful for encoding data that needs to be safely transmitted over media that are designed to deal with textual data.

PowerShell and Base64 Encoding

PowerShell provides several ways to handle base64 encoding. The most straightforward method involves using the [Convert]::ToBase64String() method for encoding and [Convert]::FromBase64String() for decoding.

Encoding a string to Base64

Here is a PowerShell script to encode the string “ScriptWizards.net” to base64:

# Define the string to encode
$plainText = "ScriptWizards.net"

# Convert the string to a byte array
$bytes = [System.Text.Encoding]::UTF8.GetBytes($plainText)

# Encode the byte array to base64
$base64Encoded = [Convert]::ToBase64String($bytes)

# Display the base64 encoded string
Write-Output $base64Encoded

Output:

U2NyaXB0V2l6YXJkcy5uZXQ=

Conclusion

PowerShell, with its robust scripting capabilities, provides straightforward methods for performing base64 encoding and decoding.

By utilising the [System.Text.Encoding]::UTF8.GetBytes() method to convert a string into a byte array and [Convert]::ToBase64String() to encode it, PowerShell allows for efficient and effective data encoding. This process is particularly useful in scenarios such as data serialisation, web development, and secure data transfer.


Recommended Reading: Base64 Decoding with PowerShell

Base64 Decoding with PowerShell

Base64 encoding is a common technique used to represent binary data in an ASCII string format. It’s often used for encoding data in various contexts, including email via MIME, storing complex data in XML or JSON, and encoding credentials in web development. Decoding base64 data is equally important to retrieve the original information. This guide will teach you the process of decoding a base64 string using PowerShell.

Understanding Base64 Encoding

Base64 encoding converts binary data into an ASCII string using a set of 64 different characters (A-Z, a-z, 0-9, +, /). This makes it suitable for transferring data over media that are designed to deal with text. However, to use the data in its original form, we need to decode it back from the base64 format.

Decoding Base64 in PowerShell

PowerShell provides a straightforward way to decode base64 strings using the [System.Convert]::FromBase64String method.

Decoding a Secret Base64 String

Suppose you have a base64-encoded string and you want to decode it to reveal the original text. Let’s assume the base64 string is U2NyaXB0V2l6YXJkcy5OZXQ=, which decodes to ScriptWizards.Net.

Here’s the PowerShell script to decode this base64 string:

# Define the base64-encoded string
$base64EncodedString = "U2NyaXB0V2l6YXJkcy5OZXQ="

# Convert the base64 string to a byte array
$base64ByteArray = [System.Convert]::FromBase64String($base64EncodedString)

# Convert the byte array back to the original string
$decodedString = [System.Text.Encoding]::UTF8.GetString($base64ByteArray)

# Output the decoded string
Write-Output $decodedString

Output:

ScriptWizards.Net

Explanation

Defining the Base64-Encoded String: The variable $base64EncodedString holds the base64-encoded string you want to decode.

Converting Base64 String to Byte Array: The [System.Convert]::FromBase64String method converts the base64-encoded string into a byte array. This method takes the base64 string as input and returns an array of bytes.

Converting Byte Array to Original String: The [System.Text.Encoding]::UTF8.GetString method converts the byte array back into the original string. This method takes a byte array as input and returns the corresponding string.

Outputting the Decoded String: Write-Output prints the decoded string to the console.

Conclusion

Decoding base64 strings in PowerShell is a simple process that leverages the .NET framework’s built-in methods for converting base64-encoded data back into its original form. This can be particularly useful for handling encoded data in various applications, such as securely transmitting information or encoding configuration files.


Recommended Reading: Base64 Encoding with PowerShell

© 2024 ScriptWizards.net - Powered by Coffee & Magic