Base64 Explained: What It Is, When to Use It, and When Not To
If you work with the web for more than a week, you'll meet Base64: long strings of seemingly random letters, numbers, and the occasional "+", "/", or "=" turning up in data URIs, JSON payloads, and tokens. It looks cryptic, but the concept is simple — and understanding it clears up a surprising amount of confusion.
What Base64 actually does
Computers store everything as binary. The problem is that many systems — email, URLs, JSON, XML — were designed for text, and raw binary data can contain bytes that those systems interpret as control characters and mangle. Base64 solves this by re-expressing binary data using just 64 safe, printable ASCII characters (A–Z, a–z, 0–9, plus two symbols). The result is text that survives any text-only channel intact.
The format is formally defined in RFC 4648. You can encode and decode any string or file instantly with our Base64 encoder and Base64 decoder.
The mistake: Base64 is NOT encryption
This is the most important point in the whole article. Base64 is encoding, not encryption. There is no key and no secret — anyone who sees a Base64 string can decode it back to the original in seconds. Never use it to "hide" passwords, API keys, or any sensitive data. It exists purely for compatibility and safe transport, not for confidentiality. If you need secrecy, you need real encryption (like AES) or hashing (like bcrypt) instead.
How the encoding works (briefly)
Base64 takes three bytes of input (24 bits) and splits them into four 6-bit groups, mapping each group to one of its 64 characters. Because three bytes become four characters, the output is about 33% larger than the input. When the input isn't a clean multiple of three bytes, "=" padding characters fill the gap. The MDN Base64 glossary has a clear visual walkthrough if you want the bit-level detail.
Where you'll meet it
- Data URIs: small images embedded directly in CSS or HTML as
data:image/png;base64,..., saving an HTTP request. - JWTs: the header and payload of a JSON Web Token are Base64URL-encoded — decode one and you'll see the parts revealed instantly.
- HTTP Basic Auth: credentials are Base64-encoded (which is exactly why Basic Auth must always run over HTTPS).
- Email attachments: MIME uses Base64 to send binary files through text-based mail protocols.
A note on size and when to avoid it
That ~33% size increase means Base64 is great for small assets but a poor choice for large files, where the bloat outweighs the convenience of inlining. Embedding a tiny icon as a data URI is smart; Base64-encoding a 2 MB hero image into your HTML is not — it delays rendering and can't be cached separately.
Bottom line
Base64 is a transport format: it makes binary data safe to move through text channels, nothing more. Reach for it to embed small assets, read tokens, and pack binary into JSON — and never mistake it for security. When you need to inspect a Base64 string in the wild, the decoder is one click away.