Base64 is one of those things every developer runs into but few stop to understand. You see it in JWTs, data URIs, email attachments, API payloads, and config files. People often assume it's some kind of encryption. It isn't. Knowing what it actually does saves you from a few embarrassing bugs.

What Base64 is

Base64 is a way to represent binary data using only printable ASCII characters. It takes arbitrary bytes and maps them onto a 64-character alphabet: A-Z, a-z, 0-9, plus + and /, with = used for padding.

The mechanics are simple. It reads your input three bytes at a time — that's 24 bits — and splits those 24 bits into four 6-bit groups. Each 6-bit value (0–63) becomes one character from the alphabet. Since three bytes become four characters, Base64 output is always about 33% larger than the input.

When your input isn't a clean multiple of three bytes, padding kicks in. One leftover byte produces two characters plus ==; two leftover bytes produce three characters plus a single =. That's why you see those trailing equals signs.

Why you'd use it

The core use case is moving binary data through channels that only reliably handle text. A few concrete examples:

  • Data URIs — embedding a small image directly in CSS or HTML with data:image/png;base64,... so it loads without a separate request.
  • Email — SMTP was built for 7-bit ASCII, so attachments get Base64-encoded via MIME.
  • JWTs — the header and payload are Base64url-encoded JSON, separated by dots.
  • Embedding secrets or certificates in config — storing a PEM key or binary blob inside a YAML or JSON field without breaking the format.
  • Basic Auth headers — Authorization: Basic followed by the Base64 of username:password.

The thing to get straight: it is not encryption

This matters enough to repeat. Base64 is encoding, not encryption. There's no key and no secret. Anyone can decode it instantly. If you put a password in a Basic Auth header, it's one trivial step away from plaintext — which is exactly why Basic Auth needs HTTPS to be safe at all.

I've seen more than one "security" measure that boiled down to Base64-ing a value and calling it obfuscated. Don't do that. If you need confidentiality, use actual encryption. Base64 is for transport, not protection.

Common gotchas

Standard vs. URL-safe

The + and / characters have special meaning in URLs, and = can cause issues too. Base64url swaps + for - and / for _, and often drops padding entirely. If you decode a JWT with a standard Base64 decoder and it chokes, this is usually why. Match the variant to the context.

Character encoding of the original string

Base64 operates on bytes, not characters. Before you encode text, you have to decide how it becomes bytes. Encode "café" as UTF-8 and you get different bytes than Latin-1 — and therefore a different Base64 string. When you decode, you need to interpret the bytes with the same charset. Mismatched encodings are a classic source of mangled accented characters and emoji.

Line wrapping

Some tools, especially older MIME-oriented ones, insert newlines every 64 or 76 characters. If your decoder doesn't strip whitespace, that breaks. Most modern libraries handle it, but command-line base64 output can surprise you.

It's bigger, not smaller

People occasionally reach for Base64 thinking it compresses data. It does the opposite — roughly a third larger. If size matters, compress first, then encode.

A short worked example

Take the string Hi!. Three characters, three bytes in ASCII: 72 105 33. In binary that's:

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

Concatenate the 24 bits and regroup into four 6-bit chunks:

010010 000110 100100 100001
  18     6      36     33

Map each to the alphabet (0=A, 1=B, ...): 18=S, 6=G, 36=k, 33=h. So Hi! becomes SGkh. No padding needed because the input was exactly three bytes.

In practice you won't do this by hand. Here's how it looks in a few languages:

// JavaScript (browser)
btoa("Hi!");                    // "SGkh"
atob("SGkh");                   // "Hi!"

// JavaScript (Node, UTF-8 safe)
Buffer.from("Hi!", "utf8").toString("base64");
Buffer.from("SGkh", "base64").toString("utf8");

// Python
import base64
base64.b64encode(b"Hi!")        # b'SGkh'
base64.b64decode("SGkh")        # b'Hi!'
base64.urlsafe_b64encode(b"Hi!")  # URL-safe variant

One warning on the browser functions: btoa and atob only handle Latin-1 byte values. Feed them a multibyte Unicode string and they throw. For anything beyond ASCII, encode to UTF-8 bytes first (via TextEncoder) and then Base64 those bytes.

When you just need a quick answer

For debugging — inspecting a JWT payload, checking what's inside a Basic Auth header, decoding a config value — you don't want to spin up a REPL. A browser tool is faster. Our Base64 Encoder / Decoder handles both directions and runs right in the page, which is handy when you'd rather not paste sensitive-looking strings into a random server somewhere.

The short version

  • Base64 turns bytes into safe ASCII text, roughly 33% larger.
  • Use it for transporting binary through text-only channels, not for security.
  • Watch the variant (standard vs. URL-safe) and the source charset (usually UTF-8).
  • Strip whitespace, mind the padding, and compress before encoding if size is a concern.

Once you internalize that it's a reversible encoding and nothing more, the whole thing stops being mysterious. It's a format decision, and like most format decisions, the bugs come from mismatching what goes in with what comes out.

Photo by Markus Spiske on Unsplash