What is Base64?

Why binary data needs text encoding, and when to use it

The Problem Base64 Solves

Computers store and transfer everything as binary — sequences of 1s and 0s. Text systems (email, JSON, HTML, URLs) are designed to carry readable text characters, not arbitrary binary data. If you try to send a raw binary file through a system built for text, the data gets corrupted.

Base64 is the standard solution: it converts any binary data into a set of 64 safe, printable ASCII characters. The result is always valid text, so it travels safely through any system that handles text.

How Base64 Works

Base64 takes every 3 bytes of input (24 bits) and breaks them into 4 groups of 6 bits each. Each 6-bit group maps to one of 64 characters: A–Z, a–z, 0–9, and two extras (typically + and /).

The result is always about 33% larger than the original — that's the cost of making binary data text-safe.

Input:  "Hi!"  (3 bytes: 72, 105, 33)
Output: "SGkh"  (4 chars)

If the input isn't a multiple of 3 bytes, padding characters (=) are added to make the output length a multiple of 4.

URL-Safe Base64

Standard Base64 uses + and / — characters that have special meaning in URLs. URL-safe Base64 replaces them with - and _, making the output safe to include in URLs and filenames without additional encoding.

Use standard Base64 for email (MIME) and data URIs. Use URL-safe Base64 for web tokens (JWT), URL parameters, and filenames.

Common Uses

  • Embedding images in HTML/CSS: src="data:image/png;base64,iVBOR..."
  • Email attachments: MIME encodes binary attachments as Base64
  • API tokens: JWTs use URL-safe Base64 for their header and payload
  • Storing binary data in JSON: JSON can't contain raw binary, so Base64 is used
  • Basic authentication: HTTP Basic Auth sends username:password as Base64

What Base64 Is Not

Base64 is encoding, not encryption. Anyone can decode a Base64 string instantly — there's no key, no secret, no protection. Don't use Base64 to "hide" sensitive data. Use it only to convert format, not to protect content.