Blazor Lab

Base64 Encoder and Decoder

Encode and decode text to/from Base64 string.

Input Text

Encode Base64 Decode Base64

Overview

The Base64 Encoder and Decoder is a simple online utility built with Blazor (C#) that allows users to convert plain text into Base64-encoded strings and decode Base64 strings back into their original text.

This tool is especially useful for developers working with APIs, authentication headers, configuration files, or data transmission where binary or special characters must be safely represented as text.


What the Tool Does

The Base64 Encoder and Decoder provides two core functions:

  • Encode to Base64

    • Converts plain text into a Base64-encoded string
  • Decode from Base64

    • Converts a Base64 string back into its original text
    • Displays an error if the input is not valid Base64

The tool validates input during decoding to ensure correctness and prevent misleading output.


How to Use the Tool

  1. Enter Text

    • Type or paste text into the input field
  2. Choose an Action

    • Click Encode Base64 to encode the text
    • Click Decode Base64 to decode a Base64 string
  3. View the Result

    • The result will appear in the output area
    • If decoding fails, an error message will be shown

Key Terms Explained

Base64 Encoding

Base64 is a binary-to-text encoding scheme that represents data using 64 ASCII characters (A–Z, a–z, 0–9, +, /). It is commonly used to safely transmit data over text-based systems such as HTTP headers and JSON.

Example:

Hello World

Encoded as:

SGVsbG8gV29ybGQ=

Encoding vs Decoding

  • Encoding transforms readable text into a Base64 string
  • Decoding reverses the process back into readable text

Invalid Base64

A Base64 string is considered invalid if:

  • It contains unsupported characters
  • Its length is incorrect
  • Padding (=) is malformed

When invalid input is detected, the decoder will return an error instead of corrupted output.


Example: Implementing Base64 Encode & Decode in C#

Below is a simple C# implementation showing how to encode and decode Base64 safely.

1. Base64 Utility Functions

using System;
using System.Text;

public static class Base64Helper
{
    public static string Encode(string plainText)
    {
        if (string.IsNullOrEmpty(plainText))
            return string.Empty;

        var bytes = Encoding.UTF8.GetBytes(plainText);
        return Convert.ToBase64String(bytes);
    }

    public static string Decode(string base64Text)
    {
        if (string.IsNullOrEmpty(base64Text))
            return string.Empty;

        try
        {
            var bytes = Convert.FromBase64String(base64Text);
            return Encoding.UTF8.GetString(bytes);
        }
        catch (FormatException)
        {
            throw new ArgumentException("Invalid Base64 input.");
        }
    }
}

2. Usage Example

string text = "Base64 is useful";

// Encode
string encoded = Base64Helper.Encode(text);

// Decode
string decoded = Base64Helper.Decode(encoded);

Encoded Output:

QmFzZTY0IGlzIHVzZWZ1bA==

Decoded Output:

Base64 is useful

Why Use Base64?

  • Safely transmits binary data as text
  • Prevents encoding issues in HTTP, JSON, and XML
  • Widely supported across platforms and languages
  • Essential for authentication headers and tokens

Common Use Cases

  • Encoding API credentials
  • Embedding binary data in JSON
  • Working with JWTs and tokens
  • Safely storing configuration values
  • Transmitting data via HTTP headers

Conclusion

The Base64 Encoder and Decoder offers a fast and reliable way to convert text to and from Base64, with built-in validation to prevent decoding errors. Whether you use the online Blazor tool or implement the logic yourself in C#, it’s an essential utility for everyday development and data handling tasks.


An unhandled error has occurred. Reload 🗙