Developer Documentation
C#
Updated September 19, 2026

Base64 Encoding and Decoding in C#

Encode and decode Base64 safely, including UTF-8 text and binary data in C#.

Base64 converts binary data to printable ASCII text. It is useful for transport and embedding, but it is an encoding format, not encryption.

Quick Start#

The following example uses the standard C# API or a widely used package for this task.

Quick start#

C#
1using System;2using System.Text;34string text = "Hello ToolPorta";5string encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(text));6string decoded = Encoding.UTF8.GetString(Convert.FromBase64String(encoded));78Console.WriteLine(encoded);9Console.WriteLine(decoded);

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Binary data#

C#
1byte[] bytes = await File.ReadAllBytesAsync("input.bin");2string encoded = Convert.ToBase64String(bytes);3byte[] restored = Convert.FromBase64String(encoded);4await File.WriteAllBytesAsync("restored.bin", restored);

Important Notes#

Base64 increases size by roughly one third; avoid it for large payloads unless the surrounding protocol requires text.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Base64 in other languages: