Base64 Encoding Explained: What It Is and How It Works
6 min read · Updated 2025
You have likely encountered Base64 strings — long strings of letters, numbers, and characters ending in ==. This guide explains what Base64 is, why it exists, how the algorithm works, and when to use it in real projects.
Why Does Base64 Exist?
Many systems (email protocols, URLs, JSON, XML) were designed to handle text, not arbitrary binary data. Binary data — like images, audio, or encrypted bytes — contains bytes that are either not printable ASCII characters or have special meaning in transport protocols (like newlines, null bytes, or control characters).
Base64 solves this: it converts any binary data into a safe string of 64 printable characters (A–Z, a–z, 0–9, +, /) that can travel safely through any text-based system.
How Base64 Encoding Works
Base64 works by processing input 3 bytes (24 bits) at a time:
- Take 3 bytes of binary data
- Split the 24 bits into four 6-bit groups
- Map each 6-bit group (0–63) to a Base64 character using the Base64 alphabet
- Output 4 characters for every 3 bytes of input
Worked Example: Encoding "Man"
| Text | M | a | n | |
|---|---|---|---|---|
| ASCII (decimal) | 77 | 97 | 110 | |
| Binary | 01001101 | 01100001 | 01101110 | |
| Combined: 010011 010110 000101 101110 | ||||
| Base64 index | 19 | 22 | 5 | 46 |
| Base64 character | T | W | F | u |
Result: TWFu — exactly 4 characters for 3 input bytes.
The = Padding
If the input is not a multiple of 3 bytes, Base64 pads the output with = signs:
- 1 remaining byte → 2 Base64 chars +
== - 2 remaining bytes → 3 Base64 chars +
= - 3 remaining bytes → 4 Base64 chars (no padding)
Size Overhead
Base64-encoded data is approximately 33% larger than the original binary (4 characters for every 3 bytes). This trade-off is acceptable when the alternative is data corruption during transport.
Common Use Cases
- Inline images in CSS/HTML:
background: url("data:image/png;base64,iVBOR...") - JSON Web Tokens (JWT): The header and payload are Base64Url encoded
- Email attachments: MIME uses Base64 to embed binary attachments
- API keys and credentials: HTTP Basic Auth encodes
user:passwordas Base64 - Data URIs: Embedding fonts or small images directly in HTML/CSS
Base64 Is NOT Encryption
Anyone can decode a Base64 string with no key required. Never use Base64 to "hide" sensitive data. Use it only for safe binary-to-text transport.
Encode and decode Base64 instantly with BrainBoost's Base64 Encoder/Decoder. Also try the Binary/Hex Converter and HTML Entity Encoder.