What is Base64?

Base64 shows up everywhere: in email attachments, data URLs, JWTs, API payloads. It looks like encryption and behaves like nothing of the sort. Here is what it actually does, why UTF-8 text trips people up, and when you should not use it.

The problem it solves

Lots of protocols from the 1970s and 80s — email above all — were designed to carry text, and specifically text made of a small set of safe ASCII characters. Binary data (an image, a zip file, a certificate) does not survive those channels: control bytes get reinterpreted, line endings get mangled, high-bit characters get stripped.

Base64 is a bridge: it re-encodes any bytes using only 64 printable characters — A-Z, a-z, 0-9, + and / — so the data can travel through text-only pipes intact. That is its entire job. It is a transport encoding, not a security measure.

How the encoding works

The mechanism is simple bit arithmetic:

  • Take 3 bytes (24 bits) of input.
  • Split those 24 bits into four 6-bit groups.
  • Map each 6-bit value (0-63) to one character in the alphabet.

Three bytes in, four characters out — which explains the famous 33% size increase. When the input is not a multiple of 3 bytes, the last group is padded with = characters (one for 2 leftover bytes, two for 1). The padding is why you sometimes see Base64 strings ending in = or ==, and why a valid length is always a multiple of 4 after padding.

A concrete example

The string Man is three bytes: 0x4D 0x61 0x6E, or in binary 01001101 01100001 01101110. Regrouped into 6-bit chunks: 010011 010110 000101 101110 — decimal 19, 22, 5, 46, which map to T, W, F, u. That is why Man encodes to TWFu.

Why Chinese (or emoji) text breaks it

This is the single most common Base64 bug. JavaScript's built-in btoa() only accepts characters whose code point is below 256 — Latin-1. Feed it 你好 and it throws, or worse, if you sneak past validation, produces garbage.

The correct order is: encode the string to UTF-8 bytes first, then Base64 those bytes. Our Base64 tool does this automatically, which is why 你好 encodes to 5L2g5aW9 and decodes back cleanly, while naive implementations produce mojibake like 4pyM-style artifacts or throw outright.

The reverse direction has the same trap: after decoding you get bytes, and those bytes must be interpreted as UTF-8 to become a readable string again.

URL-safe Base64

Standard Base64 uses + and /, both of which are meaningful in URLs (+ decodes to a space in query strings, / splits paths). The URL-safe variant swaps them for - and _. JWTs use exactly this variant — look at any token and you will see - and _ but never + or /. The padding = is also usually omitted there, since it can be inferred from the length.

When Base64 is the wrong tool

  • Hiding secrets. Base64 provides zero security — anyone can decode it in one click. Use encryption with a key, not an encoding.
  • Storing large binary files. The 33% inflation and the CPU cost of encoding add up fast. Serve binaries as binaries.
  • Compressing. Base64 usually makes data bigger. If size matters, compress first, then — if the channel requires it — Base64 the compressed bytes.
  • Comparing "encrypted" payloads. If two Base64 strings differ only in the padding, the underlying bytes are identical. Padding is format, not content.

Quick mental checks

  • Length after encoding ≈ input size × 4/3, rounded up to a multiple of 4.
  • A Base64 string containing +, / or trailing = is standard; one containing -, _ and no padding is URL-safe.
  • If decoded output looks like garbage but does not throw, the bytes were probably decoded with the wrong character encoding — try UTF-8.