Base64 is one of those things nearly every developer uses but few stop to understand. You paste an encoded blob into a config file, you see it in a JWT, you spot it in an email header — and it just works. Until it doesn't. Then you're staring at garbled output wondering why your image won't render or your token won't decode.
Let's clear it up.
What Base64 actually is
Base64 is a binary-to-text encoding. That's the whole job. It takes arbitrary bytes and represents them using a 64-character alphabet that's safe to transmit through systems designed for text.
The alphabet is the uppercase letters, lowercase letters, digits, plus + and /, giving you exactly 64 symbols. The = character is used for padding. Every 3 bytes (24 bits) of input map to 4 Base64 characters (4 × 6 bits = 24 bits). That's why Base64 output is always about 33% larger than the original data.
The single most important point: Base64 is not encryption. It's not compression. It provides zero security. Anyone can decode it instantly. If you're using it to "hide" a password, stop.
When you'd actually use it
The common thread is always the same — you have binary data and you need to move it through a channel that only reliably handles text.
- Data URIs — embedding a small image or font directly in HTML/CSS so the browser doesn't make an extra HTTP request.
- Email attachments — the original reason Base64 exists. SMTP was built for 7-bit ASCII, so MIME uses Base64 to carry binary files.
- JWTs — the header and payload of a JSON Web Token are Base64url-encoded JSON. (Note: encoded, not encrypted — you can read any JWT's payload.)
- Storing binary in JSON or XML — APIs that need to return a file in a JSON field will typically Base64-encode the bytes.
- Basic auth headers —
Authorization: Basicis justusername:passwordrun through Base64.
For quick one-off work — decoding a token, checking what's inside a data URI, encoding a small file — I'll usually just drop it into an online Base64 Encoder / Decoder rather than open a terminal. For anything repeated or scripted, do it in code.
A short worked example
Say you want to encode the string Hello, Devsly!. Here's the same operation across a few languages.
# Python
import base64
encoded = base64.b64encode(b"Hello, Devsly!")
print(encoded.decode()) # SGVsbG8sIERldnNseSE=
# decode it back
print(base64.b64decode(encoded).decode()) # Hello, Devsly!// JavaScript (Node or browser)
const encoded = btoa("Hello, Devsly!");
console.log(encoded); // SGVsbG8sIERldnNseSE=
console.log(atob(encoded)); // Hello, Devsly!# Command line
echo -n "Hello, Devsly!" | base64
# SGVsbG8sIERldnNseSE=
echo "SGVsbG8sIERldnNseSE=" | base64 --decode
# Hello, Devsly!Notice the trailing =. The input is 14 bytes, which isn't a multiple of 3, so the encoder pads the final group. That padding is meaningful — strip it and some decoders will choke.
The gotchas that bite people
1. The echo -n trap
On the command line, echo adds a trailing newline by default. If you forget -n, you encode an extra \n byte and get different output. This is a classic cause of "my hash doesn't match" bugs.
2. Base64 vs Base64url
Standard Base64 uses + and /. Those characters are unsafe in URLs and filenames. So there's a variant — Base64url — that swaps + for - and / for _, and often drops the padding entirely. JWTs use this. If you try to decode a JWT with a standard decoder, it may fail or produce junk. Know which flavour you're dealing with.
3. Character encoding, not just bytes
Base64 operates on bytes, not characters. Before encoding a string, you have to decide how to turn it into bytes — almost always UTF-8. Browser btoa() is notorious here: it throws on characters outside Latin-1, so btoa("café") breaks. The fix is to encode to UTF-8 bytes first:
// Safe Unicode encoding in the browser
const bytes = new TextEncoder().encode("café");
const b64 = btoa(String.fromCharCode(...bytes));4. Size bloat matters at scale
That 33% overhead is fine for a 2 KB icon. It's a real problem if you're inlining a 500 KB image as a data URI — you're now shipping ~665 KB, uncacheable, blocking your HTML parse. Base64 an image only when it's genuinely small and frequently reused.
5. Whitespace and line wrapping
MIME-style Base64 wraps lines at 76 characters. Some decoders handle embedded newlines, others don't. If decoding fails mysteriously, strip whitespace first.
A quick mental checklist
- Need to send binary through a text-only channel? Base64 fits.
- Need secrecy? Base64 is the wrong tool — use real encryption.
- Working with URLs, filenames, or JWTs? Use Base64url.
- Encoding on the CLI? Remember
-n. - Encoding Unicode text? Convert to UTF-8 bytes first.
None of this is complicated once you internalise the core idea: Base64 is a transport format for bytes, nothing more. Most of the pain people hit comes from treating it as something it isn't — a security measure, a string operation, or a free lunch on payload size. Keep those three myths in mind and you'll rarely get tripped up.
Photo by Markus Spiske on Unsplash