Base64 Encoding Explained: What It Is and When to Use It
Base64 is one of those things every developer uses but few think about. You’ve seen it in JWTs, data URIs, email headers, and API authentication. But what exactly is it doing, and when should you actually use it?
What Base64 does
Base64 converts binary data into a string of ASCII characters. It takes any sequence of bytes and represents it using only 64 safe characters: A-Z, a-z, 0-9, +, /, and = for padding.
The input Hello becomes SGVsbG8= in Base64.
That’s it. It’s not encryption, not compression, not hashing. It’s a safe way to represent binary data as text.
Why it exists
Many systems — HTTP headers, JSON, XML, URLs, email — were designed to handle text, not raw binary. If you try to stuff binary data (like an image or a file) into a text field, special characters will break things.
Base64 solves this by encoding binary into a safe text representation that survives any text-based transport without corruption.
Common use cases
1. Data URIs
Instead of linking to an image file, you can embed it directly in HTML or CSS:
<img src="data:image/png;base64,iVBORw0KGgo..." />
This eliminates an HTTP request. Useful for small icons and SVGs, but counterproductive for large images (Base64 increases size by ~33%).
2. API authentication
HTTP Basic Auth encodes username:password in Base64:
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
This is encoding, not encryption. Always use HTTPS when sending credentials.
3. JWTs
JSON Web Tokens use Base64URL encoding (a URL-safe variant) for the header and payload:
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.signature
Each segment before the dots is Base64URL-encoded JSON. You can decode them to inspect the token contents without verifying the signature.
4. Email attachments
MIME uses Base64 to encode file attachments in email. This is why email attachments are larger than the original files — the Base64 overhead adds approximately 33% to the size.
5. Storing binary in JSON/XML
If an API needs to accept or return binary data (like a file upload) within a JSON body, Base64 is the standard approach:
{
"filename": "document.pdf",
"content": "JVBERi0xLjQK..."
}
Base64 vs. URL encoding vs. Hex
| Encoding | Output for “Hello” | Size overhead | Use case |
|---|---|---|---|
| Base64 | SGVsbG8= | ~33% | Binary in text contexts |
| URL | Hello (no change) | Varies | Special chars in URLs |
| Hex | 48656c6c6f | 100% | Debugging, hashes |
Base64 is the most space-efficient way to represent binary as text. URL encoding is for making strings URL-safe. Hex is for human-readable byte inspection.
When NOT to use Base64
- As encryption: Base64 is trivially reversible. Anyone can decode it. It provides zero security.
- For large files: The 33% size increase matters at scale. Store files as files, not as Base64 strings in your database.
- For text that’s already safe: If your data is plain ASCII text, Base64 just makes it bigger and unreadable for no benefit.
Try it
Our Base64 Encoder/Decoder runs in your browser — paste any text or Base64 string and convert instantly. No data sent to any server, no size limits beyond your browser’s memory.