Base64 Encoding Explained: What It Is, When to Use It, When Not To
Why binary data becomes text, how the encoding works, the 33% size cost, URL-safe variants, and the security myth that keeps causing incidents.

Base64 turns up everywhere — data URLs, email attachments, API tokens, embedded images, configuration files. It is simple enough to explain in a paragraph and misused often enough to be worth a full article.
The problem it solves
Plenty of systems were designed to carry text, not arbitrary bytes. Email headers, URLs, JSON strings, XML documents and HTTP headers all have characters that mean something structural, and control bytes that break them outright. If you need to move a PNG through one of these channels, you need a way to represent arbitrary binary as safe printable characters.
Base64 is that representation. It maps every three bytes of input to four characters drawn from a 64-character alphabet: A–Z, a–z, 0–9, plus + and /, with = used as padding at the end.
How it works, briefly
Take three bytes — 24 bits. Split those 24 bits into four groups of six. Each six-bit group is a number from 0 to 63, which indexes into the alphabet. Four characters out for every three bytes in.
When the input length is not a multiple of three, the encoder pads the final group with zero bits and appends one or two = characters so decoders know how many bytes to discard.
That is the whole algorithm. It is not compression, not encryption, and not obfuscation.
The cost
Four characters for every three bytes means encoded data is about 33% larger than the original, before you count line breaks in some variants. That overhead is the main reason to avoid base64 when you have a choice.
For a small icon inlined in CSS, 33% of two kilobytes is nothing. For a two-megabyte photograph embedded in a JSON payload, it is a real penalty — and worse, base64 data inside HTML or JSON cannot be cached separately, so every page load pays for it again.
URL-safe base64
The standard alphabet includes + and /, both of which have meaning in URLs, and =, which is awkward in query strings. The URL-safe variant replaces + with -, / with _, and usually drops the padding.
This is what you see inside JSON Web Tokens, and it is why a token pasted into a standard decoder sometimes fails — you need the URL-safe decoder, or you need to convert the characters back first.
Good uses
- Small inline assets — an icon or tiny background image in a stylesheet, avoiding an extra network request.
- Email attachments — MIME has used base64 since the beginning, because SMTP is a text protocol.
- Binary fields inside JSON or XML, which have no binary type.
- Tokens and identifiers that must survive being placed in URLs and headers.
- Embedding certificates and keys in configuration files (the PEM format is base64 with header lines).
Bad uses
Large images in HTML or JSON. You pay 33% extra, lose caching, and block rendering. Serve the file from a URL.
"Hiding" sensitive data. This is the big one. Base64 is trivially reversible by anyone, including every browser's developer tools. Encoding an API key, a password or a hidden price in base64 provides exactly zero security. If it must be secret, encrypt it or keep it on the server.
Storing large binary blobs in a database text column. Use a binary column or object storage.
As a checksum. Base64 has no integrity property at all — one flipped character decodes to different data without complaint.
Practical notes
Decoders vary in how strictly they treat whitespace, missing padding and invalid characters, so if you generate base64 in one language and decode it in another, test the round trip with real data including edge cases.
And remember what the encoding tells you and does not: successfully decoding base64 proves the characters were valid, not that the resulting bytes are the file you expected. Validate the decoded content — check the file signature, the size and the type — before doing anything with it. Treating decoded data as trusted because it decoded cleanly is a classic source of upload vulnerabilities.


