env.dev

Base64 Encoding Explained

Understand Base64 encoding: how the algorithm works, when to use it, and what padding means.

Base64 is an encoding scheme that converts binary data into a string of 64 printable ASCII characters. It is not encryption — it is purely a way to represent arbitrary bytes as text.

Why It Exists

Many protocols (email, HTTP headers, URLs, XML) were designed to handle text only. Binary data like images or files cannot be embedded directly. Base64 bridges this gap by representing any byte sequence as printable characters that survive text-safe transport.

How It Works

Base64 processes input 3 bytes at a time (24 bits) and outputs 4 characters from a 64-character alphabet:

A–Z (26)  a–z (26)  0–9 (10)  + /  (2)  = (padding)
─────────────────────────────────────────────────
Input bytes:   M        a        n
Binary:     01001101 01100001 01101110
Split 6-bit: 010011  010110  000101  101110
Base64 idx:    19      22       5      46
Output:         T       W       F      u

Padding

Input is padded to a multiple of 3 bytes. If 1 byte is left over, 2 = characters are appended. If 2 bytes remain, 1 = is appended. This ensures decoders know the original length.

"M"   → "TQ=="  (1 byte → 2 chars + 2 padding)
"Ma"  → "TWE="  (2 bytes → 3 chars + 1 padding)
"Man" → "TWFu"  (3 bytes → 4 chars, no padding)

Common Use Cases

Data URIs

Embed images and fonts directly in HTML/CSS without a separate request.

data:image/png;base64,iVBOR…

Email attachments

MIME encodes binary attachments as Base64 for safe transport over SMTP.

Content-Transfer-Encoding: base64

JWT tokens

JWT header and payload are Base64URL-encoded (a URL-safe variant).

eyJhbGciOiJIUzI1NiJ9…

API credentials

HTTP Basic Auth encodes username:password as Base64 in the Authorization header.

Authorization: Basic dXNlcjpwYXNz

Base64 vs Base64URL

Standard Base64 uses + and /, which are special characters in URLs. Base64URL replaces them with - and _, and omits padding. Used in JWTs, OAuth tokens, and URL-safe identifiers.

VariantChars 62/63Padding
Standard Base64+ /Yes (=)
Base64URL- _No

Size Overhead

Base64 increases data size by approximately 33%: every 3 bytes become 4 characters. For large binary files, this overhead is worth noting when bandwidth matters.

Encode and decode Base64 strings instantly with the Base64 Encode / Decode tool.