Base64 shows up in data URIs, JWTs, email attachments, API payloads, and config files. For something so common, it's surprisingly misunderstood. People reach for it expecting security or compression and get neither. Let's clear that up.

What Base64 actually is

Base64 is a binary-to-text encoding. It takes arbitrary bytes and represents them using 64 safe ASCII characters: A–Z, a–z, 0–9, plus + and /, with = as padding. That's it. It converts bytes into a string that survives systems which only handle text.

The mechanics are simple. Base64 groups your input into 3-byte chunks (24 bits) and splits each chunk into four 6-bit values. Each 6-bit value (0–63) maps to one character in the alphabet. Three bytes in, four characters out. When your data doesn't divide evenly by three, padding = fills the gap.

Because 3 bytes become 4 characters, Base64 output is always about 33% larger than the input. That size penalty is the price you pay for text-safety.

When you'd actually use it

The core use case: you have binary data and a channel that only reliably carries text. A few concrete examples:

  • Data URIs — embedding a small image directly in CSS or HTML with data:image/png;base64,... to avoid an extra HTTP request.
  • Email (MIME) — SMTP was built for 7-bit ASCII, so attachments get Base64-encoded to pass through intact.
  • JWTs — the header and payload are Base64url-encoded JSON (more on the "url" part below).
  • Embedding credentials or keys in JSON/YAML config where raw binary would break the format.
  • HTTP Basic Auth — the Authorization header carries base64(username:password).

Notice what's not on that list: security. That last example is a perfect illustration of the biggest misconception.

Base64 is not encryption

This deserves its own heading because it bites people constantly. Base64 is fully reversible by anyone, with no key. If you Base64-encode a password and ship it, you've hidden nothing. Anyone can decode it in a browser console in two seconds.

It's also not compression. It makes data larger, not smaller. If you want smaller, use gzip. If you want secret, use actual encryption. Base64 solves exactly one problem: making bytes safe to transport as text.

Common gotchas

Standard vs. URL-safe

The + and / characters have special meaning in URLs and filenames. That's why there's a Base64url variant that swaps them for - and _, and often drops the = padding. JWTs use this. If you decode a JWT with a standard Base64 decoder and get garbage, mismatched alphabets are usually why.

Padding

Some encoders omit the trailing =, and some decoders choke without it. If you're hand-rolling or stitching together systems, test round-trips at every input length (0, 1, 2 extra bytes) to catch padding bugs.

Encoding before encoding

Base64 operates on bytes, not characters. If you Base64 a string with non-ASCII characters, the result depends on the text encoding you used first. Always go string → UTF-8 bytes → Base64 and reverse it the same way. Skipping the explicit UTF-8 step is a classic source of mojibake with emoji and accented characters.

In JavaScript, the old btoa() and atob() functions only handle Latin-1, so they break on anything outside that range:

// Broken for non-Latin1 input
btoa("café"); // throws / wrong

// Correct: encode to UTF-8 bytes first
const bytes = new TextEncoder().encode("café");
const b64 = btoa(String.fromCharCode(...bytes));

// Decode back
const decoded = new TextDecoder().decode(
  Uint8Array.from(atob(b64), c => c.charCodeAt(0))
);
console.log(decoded); // "café"

Line wrapping

MIME traditionally wraps Base64 at 76 characters with newlines. If your decoder doesn't strip whitespace, those newlines cause failures. Most modern libraries handle it, but legacy tooling may not.

A short worked example

Let's encode the string Hi! by hand to see the mechanics.

  • H = 72 = 01001000
  • i = 105 = 01101001
  • ! = 33 = 00100001

Concatenate the bits: 01001000 01101001 00100001. Now regroup into 6-bit chunks: 010010 000110 100100 100001. Convert each to decimal: 18, 6, 36, 33. Map to the Base64 alphabet (index 0 = A): S, G, k, h.

"Hi!" → "SGkh"

Three input bytes divided evenly, so no padding needed. Try Hi instead and you'll get SGk= — one = because the final group was short.

When you just need to check a value, decode a JWT payload, or sanity-check what a system produced, doing it by hand is tedious. I keep our Base64 Encoder / Decoder open in a tab for exactly these quick round-trips — paste in, flip the direction, done.

Practical rules of thumb

  • Reach for Base64 only when you need to move binary through a text-only channel.
  • Never treat it as a security measure. Encode + encrypt, not encode instead of encrypt.
  • Pick the right alphabet: standard for most things, URL-safe for URLs, filenames, and tokens.
  • Always be explicit about the text encoding (UTF-8) before and after.
  • Watch the 33% size increase — inlining a large image as a data URI can bloat your HTML more than it saves in requests.

Base64 is one of those boring, dependable primitives that quietly powers a huge amount of the web. Understand the three-bytes-to-four-characters rule, respect the alphabet differences, and keep your text encoding consistent — and it'll never surprise you.

Photo by Jake Walker on Unsplash