Base64 Encoding Explained — How It Works and When to Use It
March 22, 2026 · 8 min read
Base64 is everywhere — data URIs in CSS, email attachments, API authentication headers, JWTs. But what is it actually doing? Why does SGVsbG8gV29ybGQ= mean "Hello World"? This guide explains Base64 from the ground up, with practical examples in every language you'd use it in.
Paste text or upload a file — get Base64 output instantly.
Open Base64 Encoder/DecoderWhat Is Base64?
Base64 is a binary-to-text encoding scheme. It takes any binary data (text, images, files) and represents it using only 64 printable ASCII characters: A-Z, a-z, 0-9, +, and /, plus = for padding.
The key insight: many systems (email protocols, JSON, URLs, HTML attributes) can only handle text, not raw binary. Base64 lets you safely embed binary data inside text-only channels.
Base64 is not encryption. It's encoding — anyone can decode it. It provides zero security. Don't use it to hide secrets.
How Base64 Works — Step by Step
Let's encode the string Hi! to Base64:
Step 1: Get the ASCII byte values
Step 2: Concatenate the bits
Step 3: Split into 6-bit groups
Base64 uses 6 bits per character (2^6 = 64 possible values):
Step 4: Map to the Base64 alphabet
That's it. Hi! in Base64 is SGkh.
What About Padding?
Base64 works on 3-byte (24-bit) chunks. If the input isn't a multiple of 3 bytes, the output is padded with =:
- 1 byte input → 2 Base64 chars + ==
- 2 bytes input → 3 Base64 chars + =
- 3 bytes input → 4 Base64 chars (no padding)
Base64 Size Overhead
Every 3 bytes of input become 4 bytes of output. That's a 33% size increase. A 1 MB image becomes ~1.33 MB in Base64. This matters when you're embedding images in CSS or sending large payloads.
Base64 in JavaScript
Browser: btoa() and atob()
Warning: btoa() only works with ASCII. For Unicode text, you need to encode to UTF-8 first:
Node.js: Buffer
Base64 in Python
Base64 in the Command Line
Common Uses of Base64
1. Data URIs in HTML/CSS
Embed small images directly in your HTML or CSS, avoiding an extra HTTP request:
Best for images under 5-10 KB. Larger images are better served as separate files (the 33% size increase outweighs the saved HTTP request).
2. HTTP Basic Authentication
Remember: this is encoding, not encryption. Always use HTTPS with Basic Auth.
3. Email Attachments (MIME)
Email protocols (SMTP) only handle 7-bit ASCII text. Binary attachments are Base64-encoded in MIME format. Your email client handles this automatically.
4. JSON Payloads
JSON has no binary type. When APIs need to transfer binary data (file uploads, images, certificates), Base64-encoding it into a JSON string is the standard approach:
5. JWTs (JSON Web Tokens)
JWTs use Base64URL encoding (a variant of Base64 that's URL-safe — uses - and _ instead of + and /):
Base64 Variants
- Standard Base64 — Uses A-Za-z0-9+/ with = padding. Defined in RFC 4648.
- Base64URL — Replaces + with - and / with _. Safe for URLs and filenames. Used in JWTs.
- MIME Base64 — Same alphabet as standard, but inserts line breaks every 76 characters. Used in email.
Common Mistakes
- Using Base64 for "security" — It's trivially reversible. Use encryption (AES, RSA) for security.
- Base64-encoding large files inline — The 33% overhead adds up. A 5 MB image becomes 6.67 MB. Use file references instead.
- Forgetting UTF-8 — btoa("café") throws in the browser. Encode to UTF-8 bytes first.
- Double-encoding — Base64-encoding something that's already Base64 gives garbled results. Check if your data is already encoded before encoding it.
Try It Now
Need to encode or decode Base64 quickly? Use the free Base64 encoder/decoder on UtilShed — supports text, files, and images.
Related tools:
- Base64 Image Decoder — decode Base64-encoded images visually
- Image to Base64 Converter — convert images to data URIs
- JWT Decoder — decode and inspect JWT tokens (which use Base64URL)
- URL Encoder/Decoder — for URL-safe encoding (different from Base64)
- Hash Generator — for one-way hashing (SHA-256, MD5, etc.)