Base64 Encoding Explained: What It Is, How It Works & When to Use It
Base64 encoding is one of those technologies that's everywhere once you know to look for it. From the images embedded in HTML emails to the authentication tokens in your HTTP headers, Base64 is the universal translator between binary data and text. But what exactly is it, and when should you use a Base64 encoder? Let's break it down.
What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that converts binary data (like images, files, or raw bytes) into a string of ASCII characters. It uses a 64-character alphabet: A-Z, a-z, 0-9, +, and /. The "64" in Base64 refers to these 64 characters — each character represents exactly 6 bits of data (2⁶ = 64).
The key insight: binary data contains bytes that aren't valid text characters. If you try to transmit raw binary through a text-based protocol (like HTTP headers, JSON, or email), you'll get corruption. Base64 solves this by representing any binary data as safe, printable ASCII text.
How Base64 Works (In Plain English)
Base64 takes your input data, breaks it into 6-bit chunks, and maps each chunk to one of 64 characters. Three bytes (24 bits) of binary data become four Base64 characters. This is why Base64-encoded data is approximately 33% larger than the original — it's a trade-off: size for compatibility.
Original text: "Man"
ASCII bytes: 77, 97, 110 (0x4D, 0x61, 0x6E)
24-bit group: 01001101 01100001 01101110
6-bit chunks: 010011 010110 000101 101110
Base64 chars: T W F u
Encoded: "TWFu"
Common Base64 Use Cases
1. Embedding Images in HTML/CSS
Data URIs let you embed images directly in HTML or CSS without separate file requests. A Base64-encoded image looks like: data:image/png;base64,iVBORw0KGgo.... This is great for small icons and logos — it reduces HTTP requests. But for large images, the 33% size overhead and lack of caching make external files a better choice.
2. Email Attachments (MIME)
Email protocols were designed for text. MIME (Multipurpose Internet Mail Extensions) uses Base64 to encode binary attachments — PDFs, images, ZIP files — so they can travel through email servers without corruption.
3. JSON Web Tokens (JWT)
The header and payload of a JWT are Base64-encoded JSON objects. When you decode a JWT, you're really Base64-decoding these sections to reveal the original JSON.
4. API Authentication
HTTP Basic Authentication encodes username:password as a Base64 string in the Authorization header. Note: this is encoding, not encryption — anyone who intercepts the request can decode it. Always use HTTPS with Basic Auth.
Base64 and Unicode: The Tricky Part
JavaScript's built-in btoa() and atob() functions only work with ASCII/Latin-1 characters. If you try to Base64-encode a string with emoji, Chinese characters, or other Unicode, you'll get an error:
// ❌ This throws an error
btoa("你好世界");
// ✅ Handle Unicode correctly
btoa(unescape(encodeURIComponent("你好世界")));
A proper online Base64 encoder handles Unicode automatically — critical if you're working with international text. Our Base64 tool uses this technique to ensure correct encoding for any language.
Base64 Encoding vs Encryption
This is the #1 misconception: Base64 is encoding, not encryption. It provides zero security. Anyone can decode a Base64 string back to its original form. Never use Base64 to "protect" sensitive data — use actual encryption like AES-GCM instead.
Need to encode or decode Base64 right now?
🔒 Open Base64 Tool →