Base64 is one of those things most developers use without ever stopping to think about what's happening. You copy a data URI into some CSS, you paste a token into a header, and it just works. Until it doesn't — and then you're staring at a garbled string wondering why your image won't render.
So let's clear it up properly.
What Base64 actually is
Base64 is a way of representing binary data using only 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 mechanism is simple. It reads your input three bytes at a time — 24 bits — and splits those into four 6-bit groups. Each 6-bit group (a value from 0 to 63) maps to one character in the Base64 alphabet. Three input bytes become four output characters, which is why Base64 output is always about 33% larger than the original.
Say that again, because it matters: Base64 makes data bigger, not smaller. It is not compression. It is also not encryption — anyone can decode it instantly. It's just a safe, text-friendly representation of bytes.
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 likes text.
- Email attachments. This is where Base64 came from (MIME). SMTP was built for 7-bit ASCII, so binary attachments get Base64-encoded to pass through cleanly.
- Data URIs. Embedding a small image or font directly in HTML or CSS:
background: url(data:image/png;base64,iVBOR...). Handy for tiny assets to save an HTTP request. - JSON and APIs. JSON has no native binary type. If you need to send a file blob or raw bytes in a JSON payload, Base64 is the usual escape hatch.
- Tokens and credentials. HTTP Basic Auth encodes
username:passwordas Base64. JWTs use Base64url for their header and payload segments. - Storing binary in text-only fields. Config files, environment variables, cookies — anywhere that expects a string.
The gotchas that bite people
It's not security
I've seen Base64 used as if it hides a secret. It doesn't. A Base64 string is trivially reversible — that's the whole point. If someone sniffs an HTTP Basic Auth header, decoding it takes one line of code. Base64 protects nothing; use TLS and real auth for that.
Base64 vs Base64url
Standard Base64 uses + and /. Both of those have special meaning in URLs and filenames. So there's a variant, Base64url, that swaps them for - and _ and often drops the = padding. JWTs use it. If you decode a JWT segment with a plain Base64 decoder and it chokes, this mismatch is usually why.
Padding
The = characters at the end pad the output to a multiple of four. Some decoders are strict and reject input without correct padding; others are lenient. If you're stripping padding anywhere, make sure the decoder on the other end can cope.
Encoding of the original text
Base64 works on bytes, not characters. If your input is a string like café, you need to decide how it becomes bytes first — UTF-8, UTF-16, something else. Encode as UTF-8, decode as UTF-8, and you're fine. Mix them up and the accented characters come back mangled.
Line breaks
Older MIME standards insert a line break every 76 characters. Most modern code produces one continuous string, but if you're parsing output from certain command-line tools (looking at you, openssl base64), you may get newlines you need to strip.
A quick worked example
Let's encode the string Hi by hand to see the mechanics.
The ASCII bytes for H and i are 72 and 105. In binary that's 01001000 and 01101001 — 16 bits. Base64 wants groups of 6, so we pad to 18 bits: 010010 000110 1001, then pad the last group to 100100 with zeros.
Those three groups are the values 18, 6, and 36, which map to S, G, and k. Since we only had two input bytes (not a full three), we add one = for padding. Result: SGk=.
In code, you'd never do this by hand. In JavaScript:
// Browser-safe, UTF-8 aware
const encode = str => btoa(unescape(encodeURIComponent(str)));
const decode = b64 => decodeURIComponent(escape(atob(b64)));
encode('Hi'); // "SGk="
encode('café'); // "Y2Fmw6k="
decode('Y2Fmw6k='); // "café"In Node you'd reach for Buffer instead:
Buffer.from('café', 'utf8').toString('base64'); // 'Y2Fmw6k='
Buffer.from('Y2Fmw6k=', 'base64').toString('utf8'); // 'café'And in Python:
import base64
base64.b64encode('café'.encode('utf-8')) # b'Y2Fmw6k='When you just need to check something fast
Half the time you're not writing code — you're debugging. Someone hands you a token, a config value, or a mystery string, and you want to know what's inside it right now. Rather than spinning up a REPL, paste it into a browser-based Base64 Encoder / Decoder and read the result instantly. It's the quickest way to confirm a JWT payload or sanity-check a data URI without leaving your browser.
The short version
Base64 is a reliable, boring, essential tool. Keep these facts in your head and you'll avoid most of the pain:
- It converts binary to text-safe characters, roughly 33% larger.
- It is not encryption and not compression.
- Watch for the Base64url variant when working with URLs, filenames, or JWTs.
- Always be explicit about your character encoding — UTF-8 in, UTF-8 out.
- Padding and line breaks are the usual culprits when a decode fails.
Reach for it when you genuinely need binary-in-text. Skip it when you think you need it for security, because you don't.
Photo by Florian Olivo on Unsplash