Base64 explained: what it is, why output grows by a third, and when not to use it

How Base64 turns bytes into text, why padding exists, what the URL-safe alphabet changes, and the mistakes that cause garbled output.

Encoding · 7 min read · Published

Base64 shows up everywhere: in data: URLs, email attachments, JSON Web Tokens, HTTP Basic authentication headers and the secret fields of countless config files. It is also one of the most commonly misunderstood encodings, mostly because it looks scrambled enough to pass for encryption. This guide covers what Base64 actually does, why it behaves the way it does, and the handful of mistakes that account for most broken output.

The problem Base64 solves

Many systems were designed to carry text, not arbitrary bytes. Early email could only reliably move 7-bit ASCII; some protocols treat certain byte values as control characters; JSON has no native way to hold binary data. If you need to push an image, a key or a compressed blob through a channel like that, you need a way to represent every possible byte using only characters that survive the trip.

Base64 is that representation. It uses 64 characters that are safe almost everywhere — A–Z, a–z, 0–9, + and / — plus = for padding. Any sequence of bytes can be written using only those characters and recovered exactly.

How the encoding works

The mechanics are simple. Take the input three bytes at a time. Three bytes are 24 bits. Split those 24 bits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, and that number picks one character from the alphabet.

Take the word Man. Its bytes are 77, 97 and 110, which in binary are 01001101 01100001 01101110. Regrouped into sixes, that is 010011 010110 000101 101110 — the numbers 19, 22, 5 and 46 — which map to T, W, F and u. So Man encodes to TWFu.

This is also why Base64 output is always about 33% larger than the input: every 3 bytes become 4 characters. A 30 KB image becomes roughly 40 KB of Base64 text. That overhead is the price of text-safety, and it is the main reason you should not Base64-encode large files when a binary channel is available.

What the = padding means

Input does not always divide neatly into groups of three. When there are one or two bytes left over, the encoder pads the final group with zero bits and marks the missing bytes with =:

  • Hi (2 bytes) encodes to SGk= — one = means one byte was missing from the last group.
  • Hello (5 bytes) encodes to SGVsbG8= — five bytes is one full group plus two leftover bytes.
  • A single leftover byte produces two = signs.

Padding lets a decoder know exactly how many bytes the last group really held. Some formats, notably JWTs, drop the padding entirely because the length can be inferred from the string itself. A strict decoder may reject unpadded input, which is a common source of "invalid Base64" errors when copying tokens between tools.

Standard vs URL-safe Base64

Two characters in the standard alphabet cause trouble in URLs: + is read as a space in query strings, and / is a path separator. The URL-safe variant, defined in RFC 4648 §5, simply swaps them for - and _. The bytes FB FF encode to +/8= in standard Base64 and to -_8 in URL-safe Base64 (usually with padding removed).

Both variants carry exactly the same information, but they are not interchangeable. Decoding a URL-safe string with a standard decoder fails on the first - or _. If you are decoding a JWT segment or a value taken from a URL, switch the decoder to the URL-safe alphabet.

Base64 is not encryption

This point deserves its own heading because it causes real security incidents. Base64 has no key and no secret. Anyone who sees a Base64 string can decode it instantly, in any browser, with no special knowledge. An HTTP Basic auth header like Authorization: Basic dXNlcjpodW50ZXIy contains the username and password in plain sight — it is only safe because the connection itself is encrypted with HTTPS.

If a value needs to stay confidential, encrypt it with a real cipher and then, if you need text-safety, Base64-encode the ciphertext. Encoding alone protects nothing.

The Unicode trap

Base64 encodes bytes, not characters. Before you can Base64-encode a piece of text, the text has to be turned into bytes, and that step requires choosing a character encoding. Today the right answer is almost always UTF-8, in which é is two bytes (encoding to w6k=) and 中 is three bytes (encoding to 5Lit).

The browser's built-in btoa() function predates this convention. It only accepts characters in the Latin-1 range and throws an InvalidCharacterError on anything else — try btoa('中') in a console. Code that works around this incorrectly is the usual cause of mojibake such as é appearing where é should be: the text was encoded with one character set and decoded with another.

The Base64 Encoder on this site always converts text to UTF-8 before encoding and decodes back from UTF-8, so emoji, accented letters and CJK text round-trip correctly.

When to use Base64, and when not to

Base64 is the right choice when binary data must travel through a text-only channel: embedding a small icon in CSS, putting a binary value in JSON, sending an attachment over SMTP, or storing a key in an environment variable.

It is the wrong choice when:

  • The data is large and a binary channel exists. Uploading a file as multipart/form-data is a third smaller than sending it as Base64 inside JSON, and avoids the CPU cost of encoding and decoding.
  • You want to hide something. Use encryption, as above.
  • You want to make text URL-safe. For a query parameter, use percent-encoding instead — see URL encoding explained. Base64 makes the value unreadable for no benefit.
  • You are inlining big images in HTML or CSS. Inlined assets cannot be cached separately and grow by a third. It pays off only for very small images where saving an HTTP request matters more than size.

Quick troubleshooting

  • "Invalid character" on decode: the string probably contains whitespace or line breaks (common in PEM files and email), or it is URL-safe Base64 being decoded with the standard alphabet.
  • "Incorrect padding": the padding was stripped. Add = until the length is a multiple of four, or use a decoder that tolerates missing padding.
  • Decoded output is garbled text: either the original was binary (an image, a compressed file), or it was text in a different character encoding than the one you are decoding with.