ブログに戻る

What Is Base64? A Complete Developer Guide

Published on June 23, 2026 · 8 min read

You see Base64 every day, probably without realizing it. That JWT token in your authorization header? Base64. The tiny image embedded directly in a CSS file? Base64. The attachment in the email you just received? Base64. Even the Basic Auth credentials your browser sends are Base64-encoded. It's everywhere — quietly bridging the gap between raw binary data and the text-based systems that make up the modern web.

At its core, Base64 is a binary-to-text encoding scheme. It takes raw bytes — the kind that would break JSON, choke an email server, or get mangled by a URL parser — and converts them into a safe, predictable set of 64 ASCII characters. It's not encryption, it's not compression, and it's definitely not magic. But understanding how it works, and why it exists, is something every developer should know.

In this guide, you'll learn the mathematics behind Base64, see the encoding algorithm step by step with concrete examples, understand the padding rules that trip up so many validators, compare the major Base64 variants (standard, URL-safe, MIME), explore where you encounter Base64 in real-world web development, and learn the common mistakes developers make when working with it.

Why Base64 Exists

The problem Base64 solves is deceptively simple: binary data cannot travel safely through text-only systems. A raw byte with the value 0 (NULL) will terminate a C string. A byte with the value 13 might be interpreted as a carriage return. Special characters like quotes, angle brackets, or backslashes can break parsers. If you need to send an image, a PDF, or any arbitrary binary data through a system designed for text, you need an encoding that guarantees every output character is safe.

The origin story goes back to email. Early SMTP servers were 7-bit only — they could only handle characters in the range 0-127 (ASCII). Binary files like images or ZIP archives contain bytes in the full 0-255 range. Send one through a 7-bit email system and the high bit of every byte gets stripped, corrupting the data irreversibly. The MIME (Multipurpose Internet Mail Extensions) standard adopted Base64 as the encoding that would make binary attachments transportable through 7-bit email relays.

Today, 7-bit email relays are largely a thing of the past, but Base64 remains essential for the same fundamental reason: modern text-based protocols — JSON, XML, HTTP headers, URL query strings — cannot hold raw bytes. A JSON specification does not allow arbitrary binary data inside a string value without escaping. An HTTP header cannot contain raw bytes at all. Base64 is the universal translator that converts anything — an image, a cryptographic signature, a PDF — into a string of characters that every text-based system can handle.

The Mathematics of Base64

The math behind Base64 is elegant and worth understanding because it explains everything else — the 33% size overhead, the padding rules, and why the output length is always a multiple of 4 characters.

Base64 operates on the fundamental relationship: 3 input bytes = 4 output characters. Three bytes contain 24 bits of data (8 bits × 3 = 24). A single Base64 character represents 6 bits of data because 2^6 = 64 — hence the name. Divide 24 bits into four 6-bit groups, and you get four Base64 characters. The encoding always works in 3-byte chunks, and every 3 bytes of input expand to 4 bytes of output. That's where the 33% size increase (4/3 = 1.33×) comes from.

Each 6-bit value maps to one of 64 characters in the Base64 alphabet:

  • 0-25: Uppercase letters A-Z
  • 26-51: Lowercase letters a-z
  • 52-61: Digits 0-9
  • 62: Plus sign (+) — or hyphen (-) in URL-safe variant
  • 63: Forward slash (/) — or underscore (_) in URL-safe variant

Why 64 characters specifically? It's the sweet spot. More characters would reduce overhead but require using non-printable or problematic characters. Fewer characters would increase overhead. The 64-character alphabet uses only alphanumeric characters plus two punctuation marks — all within the safe, printable ASCII range that every text system handles.

The Encoding Algorithm Step by Step

Understanding the algorithm demystifies Base64 entirely. Let's walk through it with a complete example, encoding the word Hello into Base64.

Step 1: Convert Text to Bytes

Every character in Hello maps to an ASCII byte value. In memory, the string is stored as five bytes:

H = 72  (01001000)
e = 101 (01100101)
l = 108 (01101100)
l = 108 (01101100)
o = 111 (01101111)

Five bytes is 40 bits. Since Base64 encodes in groups of 3 bytes (24 bits), we have one full group of 3 (H-e-l, 24 bits) and one partial group of 2 (l-o, 16 bits). The partial group will need padding.

Step 2: Split Into 3-Byte Chunks

Split the 5 bytes into two groups. The first group has 3 bytes (H, e, l). The second group has 2 bytes (l, o) — which is incomplete and will require special handling.

Step 3: Split Each 24-Bit Group Into Four 6-Bit Values

Group 1 (H-e-l): 72, 101, 108
Binary concatenation: 01001000 01100101 01101100

Split into 6-bit groups:
010010 → 18 → S
000110 → 6  → G
010101 → 21 → V
101100 → 44 → s

Group 2 (l-o): 108, 111
Binary concatenation: 01101100 01101111

Split into 6-bit groups:
011011 → 27 → b
000110 → 6  → G
1111__ → Need 2 more bits → pad with zeros → 111100 → 60 → 8

Result: "SGVsbG8="

For the incomplete second group (only 2 bytes = 16 bits, but we need 24 bits to fill three 6-bit groups), the missing 8 bits are filled with zeros. The resulting third 6-bit group maps to our final character, and an equals sign is appended to indicate the padding.

Step 5: Add Padding

The output length must always be a multiple of 4. Our second group produced 3 output characters, so we add one = to reach 4. The final encoded result is SGVsbG8=. And that's howHello becomes Base64.

You can verify this yourself using our text to Base64 converter— paste "Hello" and you'll see SGVsbG8=.

Padding Rules Explained

Padding is one of the most misunderstood parts of Base64 — and where most validators and implementations stumble. The rules, however, are precise and deterministic.

Padding exists because Base64 encodes in groups of 3 bytes, but real data rarely comes in perfectly divisible-by-3 sizes. When the input has leftover bytes after grouping, those bytes still need to produce a valid 4-character output block. The equals sign (=) fills the remaining character positions:

  • 0 extra bytes (input divisible by 3): No padding. A 3-byte input produces exactly 4 characters. Example: Man TWFu
  • 1 extra byte (input mod 3 = 1): Two padding characters. The single byte has 8 bits; it produces two 6-bit groups then needs four more bits (zero-filled) for a third group, plus one equals sign. That gives 2 characters + ==. Example: M TQ==
  • 2 extra bytes (input mod 3 = 2): One padding character. The two bytes have 16 bits; they produce two 6-bit groups plus two leftover bits (zero-filled) for a third group, plus one equals. That gives 3 characters + =. Example: Ma TWE=

Three critical rules for padding: it only appears at the end of the string, itonly appears in amounts of 1 or 2 (never 3), and a single equals sign means the input had 2 extra bytes while two equals signs mean 1 extra byte. If you see padding in the middle of a string or three equals signs at the end, the data is corrupt or the string is not valid Base64 at all.

Some implementations omit padding entirely — particularly the URL-safe variant used in JWT tokens. Without padding, you can still decode the data because the length of the Base64 string implicitly tells you how many padding characters were needed. If a string has length 2 (mod 4), it needs ==. If length 3 (mod 4), it needs a single =. The decoder can reconstruct these automatically.

Base64 Variants

Not all Base64 is the same. The core algorithm is identical across variants, but the character set and formatting rules differ based on where the encoded data needs to go. Using the wrong variant can silently corrupt your data or make it unusable in the target system.

Standard Base64 (RFC 4648)

This is the default Base64 you get from most libraries and programming languages. It uses the standard alphabet: A-Z, a-z, 0-9, +, /, with = for padding. It works everywhere except contexts where + and / have special meanings — like URLs and file paths.

URL-Safe Base64 (Base64URL)

Standard Base64 uses + and /, both of which are problematic in URLs. The plus sign is interpreted as a space in query strings, and forward slashes get confused with path separators. URL-safe Base64 replaces them with - (hyphen) and _ (underscore), and often omits padding entirely since padding equals signs also cause issues in URLs. This is the variant used in JWT tokens, OAuth state parameters, and anywhere encoded data appears in a URL.

Standard:  "aGVsbG8vd29ybGQ="
URL-Safe:   "aGVsbG8vd29ybGQ"  (+, / replaced, padding removed)

// JWT tokens use URL-safe Base64 for all three segments:
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0In0.dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

MIME Base64

When Base64 is used for email attachments (MIME), the encoded output is split into lines of at most 76 characters. This was originally to keep lines short enough for 7-bit SMTP servers that had line length limits. Modern email clients still use this format for backwards compatibility, but if you're encoding data for an API, you almost certainly want the standard (unbroken) variant instead.

PEM Format

You've seen this if you've ever worked with SSL certificates or SSH keys. PEM (Privacy-Enhanced Mail) wraps Base64-encoded DER certificate data between header and footer lines like -----BEGIN CERTIFICATE-----and uses 64-character line breaks. It's standard Base64 with specific formatting, not a different encoding.

Where You Encounter Base64

Once you recognize Base64, you start seeing it everywhere. Here are the most common places you'll encounter it in day-to-day web development:

  • JWT Tokens: All three parts of a JWT (header, payload, signature) are Base64URL-encoded. The tokens you pass in Authorization: Bearer ... headers are Base64 at every level.
  • Data URIs: When you embed images directly in CSS or HTML using data:image/png;base64,iVBORw0..., the image bytes are Base64-encoded. Our image to Base64 converter handles this for any image format.
  • Email Attachments: MIME uses Base64 to encode binary attachments so they survive the journey through email servers that only speak text.
  • Canvas toDataURL(): The browser's canvas.toDataURL() method returns a Base64-encoded representation of the canvas contents — the same data URI format used for inline images.
  • Basic Authentication: The Authorization: Basic dXNlcjpwYXNz header contains Base64-encoded username:password credentials. You can decode them with our Basic Auth decoder.
  • Binary Data in JSON: JSON cannot hold raw bytes, so binary fields (like cryptographic signatures, hashed values, or file contents in API payloads) are Base64-encoded into string values.

Common Mistakes

Base64 seems simple, but there are pitfalls that trip up developers constantly. Here are the most common ones:

  • Confusing Base64 with encryption. This is the biggest misconception. Base64 is encoding, not encryption. It provides zero security — anyone can decode it instantly. If you see a Base64 string with sensitive data, that data is effectively in plaintext. Never use Base64 as a substitute for proper encryption.
  • Forgetting the 33% size overhead. Encoding data to Base64increasesits size by roughly one-third. A 1 MB file becomes approximately 1.33 MB when Base64-encoded. If you're building an API that sends binary data as Base64 in JSON, factor this overhead into your bandwidth calculations.
  • Using standard Base64 in URLs. Standard Base64 uses + and /, which break in URLs. If your encoded data will appear in a query parameter, path segment, or fragment, always use the URL-safe variant. Our Base64 URL encoder handles the conversion automatically.
  • Assuming all Base64 is padded.Many implementations — especially in JWT tokens and URL-safe contexts — omit padding entirely. If your decoder rejects unpadded Base64, it's not necessarily invalid data; it's just a different convention. Use our Base64 validator to identify which variant you're dealing with.
  • Not realizing Base64 is not compression.Base64 expands data, it doesn't compress it. If you need to reduce data size, compress first (using gzip, for example) and then Base64-encode the compressed bytes. Using Base64 alone to "save space" will do the opposite.

Base64 vs Other Encodings

Base64 isn't the only way to represent binary data as text. Understanding how it compares to the alternatives helps you choose the right encoding for the job.

Base64 vs Hexadecimal

Hexadecimal represents each byte as two characters (00-FF), giving a 100% size increase. Base64's 33% overhead makes it far more efficient for transporting data. However, hex has a major advantage: you can read individual byte boundaries at a glance. Each pair of hex characters is exactly one byte, which makes hex the go-to format for debugging, cryptographic hashes, and color codes. Our Base64 to Hex converter lets you switch between the two instantly.

Same data, different encodings:
Bytes:   72  101  108  108  111  (5 bytes)
Hex:     48  65   6C   6C   6F   (10 chars, 100% overhead)
Base64:  SGVsbG8=               (8 chars, 60% overhead in this case)

Base64 vs Base32

Base32 uses a 32-character alphabet (A-Z, 2-7) with 5 bits per character, producing a larger overhead (~60%). It's less efficient than Base64 but has the advantage of being case-insensitive and avoiding visually ambiguous characters (like 1/l/I or 0/O). You'll see Base32 in QR codes, OTP secrets, and systems where human readability and error correction matter more than size efficiency.

When to Use Each

  • Base64: Transporting binary data through text protocols (JSON, XML, HTTP headers, email). Maximum space efficiency for a safe ASCII encoding.
  • Hexadecimal: Debugging, cryptographic hashes, color codes, and any time you need to inspect individual byte values. Twice the size of the original data.
  • Base32: Human-readable tokens, OTP secrets, QR codes, and any scenario where visual ambiguity or case-sensitivity would cause errors.

Try Our Base64 Tools

Understanding the theory is important, but working with Base64 directly is the best way to internalize it. We've built a complete suite of free Base64 tools to help:

  • Text to Base64 Converter — Encode and decode any text string to and from Base64. See the encoding happen in real time.
  • Image to Base64 — Convert any image file to a Base64 data URI, ready to embed directly in HTML or CSS.
  • Base64 Validator — Check if a string is valid Base64, identify which variant it uses, and see exactly where any problems are.
  • Base64 to Hex — Convert between Base64 and hexadecimal to see the raw byte values behind any encoded string.

These tools are all free, run entirely in your browser, and don't upload your data to any server. They're built for developers who want to understand and work with Base64 efficiently, without hunting through documentation or writing throwaway scripts.

Keep Reading