Base64 Encoding Explained: When and Why to Use It
Base64 is one of those things every developer encounters but few fully understand. Let's demystify it.
What is Base64?
Base64 is a binary-to-text encoding scheme. It converts binary data (bytes) into a string of 64 printable ASCII characters: A-Z, a-z, 0-9, +, and /. The "=" character is used for padding.
The name "Base64" comes from the fact that it uses 64 characters to represent data. Each Base64 character encodes exactly 6 bits (2^6 = 64), so 3 bytes (24 bits) of input become 4 Base64 characters.
How It Works
Input: "Hi"
Bytes: 72 105 (ASCII values)
Binary: 01001000 01101001
Groups: 010010 000110 1001xx
Base64: S G k=
Result: "SGk="The "=" at the end is padding because the input wasn't evenly divisible by 3 bytes.
Common Use Cases
- Data URIs — Embedding small images directly in HTML/CSS:
data:image/png;base64,iVBOR... - Email attachments — MIME encoding uses Base64 to safely transmit binary files over text-based email protocols
- API payloads — Sending binary data (images, PDFs) inside JSON, which only supports text
- Basic HTTP auth — The Authorization header encodes
username:passwordin Base64 - JWT tokens — The header and payload sections of JWTs are Base64URL-encoded JSON
Base64 is NOT Encryption
This is a critical misconception. Base64 provides zero security.It's a reversible encoding, not encryption. Anyone can decode a Base64 string. Never use it to "protect" passwords, API keys, or sensitive data.
The 33% Size Overhead
Base64 encoding increases data size by approximately 33%. Three bytes of input become four bytes of output. This means a 1 MB image embedded as a data URI becomes ~1.33 MB. For small assets (icons, tiny images), the trade-off of eliminating an HTTP request is worth it. For larger files, it's almost always better to serve them separately.
Base64 vs. Base64URL
Standard Base64 uses + and /, which are special characters in URLs. Base64URL replaces them with - and _ and omits padding. This variant is used in JWTs and anywhere Base64 data appears in URLs.
Try It Yourself
Experiment with our Base64 Encoder / Decoder — encode text, decode strings, and swap between modes instantly. Everything runs in your browser with full UTF-8 support.