How do I encode and decode a base64 string?
Categories:
Encoding and Decoding Base64 Strings in C#

Learn how to convert binary data to a Base64 string and back again using C#'s built-in functionalities, ensuring safe data transmission and storage.
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It's commonly used to transmit data over mediums that are designed to handle text, such as email, or to embed binary data within text-based formats like JSON or XML. This article will guide you through the process of encoding and decoding Base64 strings in C# using the Convert
class.
Understanding Base64 Encoding
Base64 encoding takes 3 bytes of binary data (24 bits) and represents them as 4 Base64 characters. Each Base64 character represents 6 bits of data. This process ensures that the resulting string contains only characters that are universally safe for text-based systems (A-Z, a-z, 0-9, +, /, and = for padding). This expansion means that the encoded string will be approximately 33% larger than the original binary data.
flowchart LR A[Original Data (Binary)] --> B{Convert to Bytes} B --> C["System.Text.Encoding.UTF8.GetBytes(string)"] C --> D{Base64 Encode} D --> E["Convert.ToBase64String(byte[])"] E --> F[Base64 Encoded String] F --> G{Base64 Decode} G --> H["Convert.FromBase64String(string)"] H --> I{Convert to String} I --> J["System.Text.Encoding.UTF8.GetString(byte[])"] J --> K[Decoded Data (Original String)]
Flowchart of Base64 Encoding and Decoding Process
Encoding a String to Base64
To encode a regular string into a Base64 string, you first need to convert the string into a byte array. The most common way to do this is by using a character encoding, such as UTF-8. Once you have the byte array, you can use the Convert.ToBase64String()
method to perform the Base64 encoding.
using System;
using System.Text;
public class Base64Encoder
{
public static string EncodeToBase64(string plainText)
{
// Step 1: Convert the string to a byte array using UTF-8 encoding
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
// Step 2: Convert the byte array to a Base64 string
string base64String = Convert.ToBase64String(plainTextBytes);
return base64String;
}
public static void Main(string[] args)
{
string originalString = "Hello, Base64 World!";
string encodedString = EncodeToBase64(originalString);
Console.WriteLine($"Original: {originalString}");
Console.WriteLine($"Encoded: {encodedString}");
}
}
C# code to encode a string to Base64.
Encoding
when converting strings to bytes and vice-versa to avoid issues with different character sets. UTF-8 is generally recommended for its broad compatibility.Decoding a Base64 String
Decoding a Base64 string is the reverse process. You start with the Base64 encoded string, use Convert.FromBase64String()
to convert it back into a byte array, and then convert that byte array back into a readable string using the same character encoding (e.g., UTF-8) that was used for encoding.
using System;
using System.Text;
public class Base64Decoder
{
public static string DecodeFromBase64(string base64EncodedData)
{
// Step 1: Convert the Base64 string to a byte array
byte[] base64EncodedBytes = Convert.FromBase64String(base64EncodedData);
// Step 2: Convert the byte array back to a string using UTF-8 encoding
string plainText = Encoding.UTF8.GetString(base64EncodedBytes);
return plainText;
}
public static void Main(string[] args)
{
string encodedString = "SGVsbG8sIEJhc2U2NCBXb3JsZCE="; // "Hello, Base64 World!" encoded
string decodedString = DecodeFromBase64(encodedString);
Console.WriteLine($"Encoded: {encodedString}");
Console.WriteLine($"Decoded: {decodedString}");
}
}
C# code to decode a Base64 string back to its original form.
Convert.FromBase64String()
is not a valid Base64 string, it will throw a FormatException
. Always handle this exception if you are processing user-provided or external Base64 strings.Practical Applications and Considerations
Base64 encoding is not encryption; it's merely an encoding scheme. The data is still easily readable once decoded. For security, combine Base64 with encryption. Common use cases include embedding images in HTML/CSS, transmitting authentication tokens (like JWTs), or storing small binary files in databases as text.