Developers · 6 min read
Base64 Encoding Explained: What It Is, Why It Exists and When Not to Use It
Base64 shows up everywhere: in image data URIs, email attachments, API payloads and the middle section of every JWT. It looks like encryption, but it is nothing of the kind. It is a way to carry binary data through systems that only understand text. This guide explains how it works, what it costs, and the mistakes people make with it.
The problem Base64 solves
Many protocols were designed for plain text. Email (SMTP), JSON and URLs can break or corrupt arbitrary bytes: a stray null byte, a newline in the wrong place or an unexpected character encoding can ruin a file in transit. Base64 sidesteps the problem by representing any sequence of bytes using only 64 safe characters: A to Z, a to z, 0 to 9, plus and slash, with equals signs for padding.
Because those characters survive every text channel unchanged, you can embed a PNG inside a JSON field or attach a PDF to an email and be sure the receiver can reconstruct the exact original bytes.
How the encoding works
Base64 reads the input three bytes (24 bits) at a time and splits those 24 bits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, which is looked up in the 64-character alphabet. Three bytes in, four characters out.
When the input length is not a multiple of three, the last group is padded with zero bits and the output is padded with one or two equals signs so the length stays a multiple of four. That is why you often see == or = at the end of a Base64 string.
- The text "Man" (bytes 77, 97, 110) becomes "TWFu".
- One leftover byte produces two characters and "==" padding.
- Two leftover bytes produce three characters and "=" padding.
The cost: roughly 33% more data
Four output characters for every three input bytes means Base64 data is about 33 percent larger than the original, before any line breaks. A 300 KB image becomes about 400 KB when inlined as a data URI.
That overhead is the main reason to avoid Base64 for large assets on web pages. An inlined image cannot be cached separately from the page, and it delays rendering of everything after it. Inlining makes sense for tiny icons where saving an HTTP request outweighs the size penalty; for anything larger, serve a normal file.
Base64 is not encryption
Anyone can decode Base64 instantly, with no key. Treating it as a way to hide passwords, API keys or personal data is a common and serious mistake. If a configuration file, cookie or log line contains Base64 text that looks scrambled, assume it is fully readable.
The same applies to the payload of a JSON Web Token: it is only Base64URL encoded, not secret. The signature protects it from tampering, but anyone holding the token can read the claims. Use real encryption, such as AES-GCM or TLS, when confidentiality matters.
Base64URL and other variants
The standard alphabet contains plus and slash, which have special meanings in URLs and file names. Base64URL swaps them for minus and underscore and usually drops the padding, making the result safe to place in a query string, a path segment or a JWT. If a decoder rejects a string that looks fine, check which variant it expects.
Another frequent source of bugs is character encoding. Base64 operates on bytes, not characters, so you must decide how text is converted to bytes first. In JavaScript, btoa fails on characters outside Latin-1, so encode text as UTF-8 bytes before converting. Non-ASCII text decoded with the wrong encoding is the usual cause of garbled accents and emoji.
Practical tips
A few habits prevent most Base64 problems:
- Strip whitespace and line breaks before decoding strings copied from emails or logs.
- Never log full Base64 credentials; Basic authentication headers are Base64 of "user:password" and trivially reversible.
- Prefer binary uploads (multipart) over Base64 in JSON for large files.
- Validate decoded output size before processing user-supplied data to avoid memory abuse.
Frequently asked questions
- Is Base64 secure?
- No. It is a reversible encoding with no key. Anyone can decode it, so never use it to protect secrets.
- Why does Base64 end with equals signs?
- They are padding. Base64 works in groups of three bytes, and when the input does not divide evenly the output is padded to a multiple of four characters.
- Why is my decoded text garbled?
- The text was probably encoded using a different character encoding than the decoder assumes. Make sure both sides use UTF-8, and that you are not mixing standard Base64 with the URL-safe variant.