What is Base64? Encoding Explained Simply
Base64 is one of the most widely used encoding schemes on the web. If you've ever seen a long string of letters and numbers in a URL, email, or CSS file, chances are it was Base64. Here's how it works and why it matters.
Table of Contents
What is Base64?
Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters. It takes any binary input — an image, a file, raw bytes — and converts it into a safe text string that can be transmitted through text-based systems.
The name "Base64" comes from the fact that it uses a set of 64 characters to represent data. Each character encodes exactly 6 bits of information (since 2⁶ = 64).
Base64 is defined in RFC 4648 and is used across the web in email (MIME), data URIs, APIs, JWT tokens, and much more.
Why Does Base64 Exist?
Many systems were designed to handle text only, not raw binary data. For example:
- Email (SMTP) — was originally designed for 7-bit ASCII text. Sending an image directly would corrupt it.
- JSON — has no native way to represent binary data. You can't put raw bytes in a JSON string.
- URLs — only support a limited set of characters. Binary data would break the URL structure.
- HTML/CSS — are text formats. Embedding an image inline requires a text representation.
Base64 solves this by converting binary data into a safe text representation that these systems can handle without corruption or misinterpretation.
How Base64 Encoding Works
The encoding process follows these steps:
- Take the input data and read it as a stream of bytes (8 bits each)
- Group the bits into chunks of 6 bits (since 64 = 2⁶)
- Map each 6-bit group to a character from the Base64 alphabet
- If the input length isn't divisible by 3, add padding (
=) to make the output a multiple of 4
The key insight: 3 bytes of input (24 bits) become 4 Base64 characters (24 bits). This is why Base64 always increases the data size by approximately 33%.
The Base64 Alphabet
The standard Base64 alphabet consists of 64 characters:
| Range | Characters | Values |
|---|---|---|
| Uppercase | A–Z | 0–25 |
| Lowercase | a–z | 26–51 |
| Digits | 0–9 | 52–61 |
| Special | + and / | 62–63 |
| Padding | = | Used for alignment |
There's also a "URL-safe" variant that replaces + with - and / with _, since + and / have special meaning in URLs.
Step-by-Step Encoding Example
Let's encode the string Hi! to Base64:
Step 1: Convert to ASCII values
H = 72, i = 105, ! = 33
Step 2: Convert to binary (8 bits each)
01001000 01101001 00100001
Step 3: Regroup into 6-bit chunks
010010 000110 100100 100001
Step 4: Convert to decimal
18 6 36 33
Step 5: Map to Base64 alphabet
S G k h
Result: "Hi!" → "SGkh"Since "Hi!" is exactly 3 bytes, it maps cleanly to 4 Base64 characters with no padding needed.
Understanding Padding (=)
Base64 works on groups of 3 bytes → 4 characters. When the input isn't divisible by 3, padding is needed:
| Input bytes | Remainder | Padding | Example |
|---|---|---|---|
| Divisible by 3 | 0 | None | "Hi!" → "SGkh" |
| 1 byte remaining | 1 | == | "H" → "SA==" |
| 2 bytes remaining | 2 | = | "Hi" → "SGk=" |
The = padding ensures the encoded output is always a multiple of 4 characters, which makes decoding straightforward.
Common Uses of Base64
Base64 is everywhere in modern software development:
- Data URIs — Embed images directly in HTML or CSS:
data:image/png;base64,iVBOR... - Email attachments (MIME) — Binary files are Base64-encoded before being attached to emails
- JWT tokens — JSON Web Tokens use Base64URL encoding for their header and payload
- API authentication — HTTP Basic auth sends credentials as
base64(username:password) - Storing binary in JSON/XML — Certificates, keys, and binary blobs encoded as text
- Source maps — JavaScript source maps use Base64-encoded VLQ for mappings
Base64 in Code
Most languages have built-in Base64 support. Here are some examples:
JavaScript (Browser & Node.js)
// Encode
const encoded = btoa("Hello, World!");
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="
// Decode
const decoded = atob("SGVsbG8sIFdvcmxkIQ==");
console.log(decoded); // "Hello, World!"Python
import base64
# Encode
encoded = base64.b64encode(b"Hello, World!")
print(encoded) # b'SGVsbG8sIFdvcmxkIQ=='
# Decode
decoded = base64.b64decode(encoded)
print(decoded) # b'Hello, World!'Command Line
# Encode (Linux/macOS)
echo -n "Hello, World!" | base64
# SGVsbG8sIFdvcmxkIQ==
# Decode
echo "SGVsbG8sIFdvcmxkIQ==" | base64 --decode
# Hello, World!Common Misconceptions
There are several things people often get wrong about Base64:
❌ "Base64 is encryption"
Base64 is not encryption. It provides no security whatsoever. Anyone can decode a Base64 string instantly. Never use it to hide sensitive data.
❌ "Base64 compresses data"
Base64 actually increases data size by about 33%. Three bytes become four characters. It's encoding, not compression.
❌ "Base64 is only for images"
While data URIs for images are common, Base64 is used for any binary data — files, certificates, authentication tokens, and more.
When Not to Use Base64
While Base64 is useful, it's not always the right choice:
- Large files — The 33% size increase adds up. For large images or files, serve them directly instead of embedding as Base64.
- Security — Never rely on Base64 to protect sensitive data. Use proper encryption (AES, RSA) instead.
- Performance-critical paths — Encoding and decoding has a CPU cost. For high-throughput systems, consider binary protocols.
- Images in web pages — For images larger than a few KB, separate files are better because browsers can cache them independently.
A good rule of thumb: use Base64 when you must transmit binary data through a text-only channel. If you have the option to send binary directly, prefer that.
Encode & Decode Base64 Instantly
Use our free Base64 Encoder/Decoder tool to encode text to Base64 or decode Base64 strings — right in your browser with no data uploaded to any server.
Try Base64 Encoder/Decoder →References
- Josefsson, S. (2006). The Base16, Base32, and Base64 Data Encodings. RFC 4648, IETF. https://datatracker.ietf.org/doc/html/rfc4648
- Freed, N. & Borenstein, N. (1996). Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies. RFC 2045, IETF. https://datatracker.ietf.org/doc/html/rfc2045
- Mozilla Developer Network. btoa() global function. https://developer.mozilla.org/en-US/docs/Web/API/btoa
- Mozilla Developer Network. Data URLs. https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs
- Python Software Foundation. base64 — Base16, Base32, Base64, Base85 Data Encodings. https://docs.python.org/3/library/base64.html