Base64 Explained: What It Is and When to Use It
Base64 is a binary-to-text encoding scheme that represents arbitrary binary data using only 64 printable ASCII characters. It was not invented for security — it was invented because many older protocols (email, HTTP headers, XML) were designed to carry text, and binary bytes routinely broke them. Understanding when Base64 helps and when it just adds bloat will save you hours of debugging.
Why Binary-to-Text Encoding Exists
The root problem is that not all bytes are safe to transmit in all contexts. A null byte (0x00) terminates strings in C. Bytes above 127 are interpreted differently across character encodings. SMTP servers historically rejected non-ASCII bytes. Line endings (\r\n vs \n) got mangled in transit.
Base64 sidesteps all of this by mapping every possible sequence of bytes to a fixed set of 64 characters: A–Z, a–z, 0–9, +, and /, plus = as a padding character. Every character in that set is printable, unambiguous in ASCII, and safe across virtually all text-handling systems built since the 1980s.
The name comes directly from the alphabet size. Base16 (hex) uses 16 symbols; Base32 uses 32; Base64 uses 64. A larger alphabet means more bits can be packed into each character, which is why Base64 is more space-efficient than hex (which doubles the byte count instead of adding 33%).
How the Encoding Works
Base64 works by reading input 3 bytes (24 bits) at a time and splitting those 24 bits into four 6-bit groups. Each 6-bit value (0–63) maps to one character in the alphabet. If the input length is not divisible by 3, padding characters (=) are appended to round up to the nearest 4-character block.
A concrete example: encode the ASCII string Man.
M= 0x4D = 01001101,a= 0x61 = 01100001,n= 0x6E = 01101110- Combined: 010011 010110 000101 101110
- Decimal: 19 22 5 46
- Characters: T W F u → TWFu
No information is lost. Base64 is fully reversible — decode TWFu and you get Man back exactly.
The 33% Overhead — and Why It Matters
Every 3 bytes of input become 4 bytes of Base64 output. That is a 4/3 ratio, or roughly 33% size increase. For a 1 MB image, expect about 1.37 MB of Base64 text.
This overhead is not a bug — it is an unavoidable consequence of the encoding math. But it is a cost worth tracking. Inline Base64 images in CSS or HTML skip an HTTP request, which can be a net win for small icons. For large assets, the 33% bloat plus the inability to cache the embedded resource independently usually makes a separate file URL the better choice.
| Original Size | Base64 Output | Overhead |
|---|---|---|
| 100 bytes | 136 bytes | +36 bytes |
| 10 KB | ~13.6 KB | +3.6 KB |
| 100 KB | ~136 KB | +36 KB |
| 1 MB | ~1.37 MB | +370 KB |
Rule of thumb: inline Base64 makes sense for assets under roughly 5–10 KB. Above that, the cache and bandwidth costs compound faster than the saved round-trip pays back.
Data URIs: Base64 in Practice
A data URI embeds file content directly inside a URL. The format is:
data:[<mediatype>][;base64],<data>
For a small PNG icon, a data URI looks like:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...
Common uses include:
- CSS backgrounds: inline small icons or patterns to eliminate HTTP requests
- HTML email: embed images because many email clients block external URLs
- SVG fonts: bundle font files directly inside SVG documents for offline use
- JSON APIs: transmit binary file content (PDFs, images) inside a JSON field without multipart encoding
Browsers support data URIs for src and href attributes, CSS url(), and <img> tags. There is a practical size limit — very long data URIs can cause rendering delays and some older browsers cap them around 32 KB.
Base64url: The URL-Safe Variant
Standard Base64 uses + and / as the 62nd and 63rd characters. Both are special in URLs: + means a space in form-encoded data, and / is a path separator. Put standard Base64 in a URL query string and those characters corrupt the value.
Base64url solves this by replacing + with - and / with _. Padding (=) is also typically omitted since it can confuse query parsers. Everything else is identical.
You will encounter base64url in:
- JWTs (JSON Web Tokens) — the header, payload, and signature are all base64url-encoded, joined by dots
- OAuth 2.0 PKCE — the code challenge is a base64url-encoded SHA-256 hash
- URL-safe identifiers — random tokens embedded in links (password resets, email confirmations)
If you are decoding a JWT by hand and your Base64 decoder throws an error, the fix is almost always to swap - back to + and _ back to / before decoding, and to add padding if the string length is not a multiple of 4.
What Base64 Is Not
Base64 is not encryption. It is not a hash. It is not even a weak form of obfuscation for any practical purpose — every developer on earth recognizes a Base64 string on sight, and decoding it is one click. The encoded data is every bit as readable as the original, just in a different representation.
This distinction matters in real systems. Storing a password as Base64 in a database provides zero protection — an attacker who reads the database column simply decodes it. Sending credentials in an HTTP header as Authorization: Basic <base64> (HTTP Basic Auth) is only safe over TLS, because the credentials are trivially reversible. Never use Base64 as a substitute for hashing, signing, or encrypting sensitive data.
Quick Encoding Reference
| Use Case | Variant | Padding | Notes |
|---|---|---|---|
| Email attachments (MIME) | Standard | Yes | Lines wrapped at 76 chars per RFC 2045 |
| Data URIs in HTML/CSS | Standard | Yes | No line breaks; single continuous string |
| JWTs | base64url | No | Three dot-separated base64url segments |
| URL query parameters | base64url | Optional | Avoids percent-encoding of + and / |
| Binary in JSON | Standard or url | Yes | Common in REST APIs for file upload fields |
Questions fréquentes
Does Base64 make data smaller?+
No — it makes it larger by about 33%. Base64 trades size for compatibility: the encoded output uses only printable ASCII characters, which makes it safe to transmit in systems that cannot handle raw binary bytes.
Can I use Base64 to hide sensitive information?+
No. Base64 is trivially reversible and provides no security. Anyone who sees the encoded string can decode it instantly. Use proper encryption for sensitive data and cryptographic hashing for passwords.
What is the difference between Base64 and base64url?+
Base64url replaces the + character with - and the / character with _, and typically omits = padding. This makes the output safe to embed in URLs and filenames without percent-encoding. JWTs use base64url throughout.
Why does my Base64 string end with one or two equals signs?+
The = characters are padding. Base64 encodes input in 3-byte chunks, producing 4 output characters per chunk. If the input length is not divisible by 3, one or two = characters are added to complete the final 4-character block.
When should I inline a Base64 image instead of using a file URL?+
Inlining makes sense for small assets — roughly under 5–10 KB — where the saved HTTP round-trip outweighs the 33% size increase and loss of caching. For larger images, a separate file URL with caching headers is almost always faster.