Base64 shows up everywhere once you start looking: data URIs, JWTs, email attachments, API payloads, config files. It's one of those things most developers use daily without ever stopping to understand what it's actually doing. Worth fixing that, because the misunderstandings cause real bugs.
What Base64 actually is
Base64 is an encoding scheme that represents binary data using 64 printable ASCII characters: A-Z, a-z, 0-9, plus + and /, with = used for padding. That's it. It takes arbitrary bytes and turns them into text that survives being passed through systems that only expect text.
The mechanics are simple. You take three bytes (24 bits), split them into four 6-bit chunks, and map each chunk to one of those 64 characters. Three bytes in, four characters out — which is why Base64 output is always about 33% larger than the input. When your data doesn't divide evenly by three, the padding = characters fill the gap.
The key thing to internalise: Base64 is not compression and it is not encryption. It makes data bigger, and anyone can decode it in a second. It's about transport, not security.
When you'd actually use it
The common thread is always the same: you need to move binary data through a channel that only safely handles text.
- Data URIs — embedding a small image or font directly in HTML or CSS with
data:image/png;base64,...to save an HTTP request. - Email (MIME) — attachments get Base64-encoded because SMTP was designed for 7-bit ASCII.
- JWTs — the header and payload are Base64url-encoded JSON (more on the "url" part shortly).
- Basic Auth — the
Authorization: Basicheader is justuser:passwordrun through Base64. - Storing binary blobs in JSON or XML — since those formats are text, binary needs encoding first.
Notice Basic Auth there. The credentials are Base64-encoded, not encrypted. Anyone who intercepts that header reads your password instantly. That's why Basic Auth over plain HTTP is a cardinal sin — it offers zero protection on its own.
The gotchas that bite people
Standard vs URL-safe Base64
Standard Base64 uses + and /. Both are problematic in URLs and filenames — / is a path separator and + gets interpreted as a space in query strings. So there's a URL-safe variant that swaps them for - and _ respectively, and often drops the padding.
This trips people up constantly with JWTs. If you grab a token and try to decode its parts with a standard Base64 decoder, it may choke or produce garbage because JWTs use Base64url. Make sure your decoder matches the variant you're dealing with.
Encoding without specifying the character set
Base64 operates on bytes, not characters. If you encode a string, you first need to turn it into bytes — and that means choosing an encoding, usually UTF-8. Skip this step and you'll get inconsistent results across platforms, especially with non-ASCII text. Emoji and accented characters are where this blows up.
Treating it as a security measure
I'll say it again because people keep doing it: encoding a secret in Base64 does not protect it. If you see a password or API key "hidden" in Base64 in a config file or client-side code, it is effectively plaintext.
Line breaks in the output
Some implementations (looking at you, older OpenSSL and MIME) insert line breaks every 64 or 76 characters. If you feed that into a decoder that doesn't strip whitespace, you get errors. When things fail mysteriously, check for stray newlines.
A worked example
Say you want to encode the string Hi!. In UTF-8 that's three bytes: 72 105 33, or in binary:
H i !
01001000 01101001 00100001Regroup those 24 bits into four 6-bit chunks:
010010 000110 100100 100001
18 6 36 33Map each number to the Base64 alphabet (0=A, 1=B, ... 25=Z, 26=a, ...): 18 is S, 6 is G, 36 is k, 33 is h. Result: SGkh. Three bytes, four characters, no padding needed because it divided evenly.
In code, you rarely do this by hand. Here's the same thing in a few languages:
// JavaScript (browser)
btoa('Hi!') // "SGkh"
atob('SGkh') // "Hi!"
// Node.js (handles binary properly)
Buffer.from('Hi!').toString('base64') // "SGkh"
Buffer.from('SGkh', 'base64').toString() // "Hi!"
# Python
import base64
base64.b64encode(b'Hi!') # b'SGkh'
base64.b64decode('SGkh') # b'Hi!'
base64.urlsafe_b64encode(b'Hi!') # URL-safe variantOne warning on the JavaScript btoa/atob pair: they only work reliably with Latin-1 strings. Pass them anything with multibyte UTF-8 characters and they throw or corrupt the data. For real text you need to convert through TextEncoder first, or just use the Node Buffer approach on the server.
Quick debugging without writing code
When you're poking at a JWT payload, inspecting a data URI, or trying to figure out what's inside a Base64 blob from a log file, firing up a REPL is overkill. Paste it into a Base64 Encoder / Decoder and read it instantly — handy for checking whether a value is what you expect before you go rewriting application code. I use it constantly when reverse-engineering someone else's API responses.
Rules of thumb to remember
- Base64 is for transport, never for security or compression.
- Output is roughly 33% larger than input — don't Base64 huge files into JSON and wonder why payloads are bloated.
- Always encode text to UTF-8 bytes first.
- Know whether you need standard or URL-safe — JWTs and URLs want the URL-safe variant.
- Strip or expect whitespace depending on the source.
None of this is complicated once you see the moving parts. The bugs come from forgetting that Base64 works on bytes, not characters, and from assuming it does more than it does. Keep those two facts in mind and you'll avoid the vast majority of Base64 headaches.
Photo by Jake Walker on Unsplash