Base64 is one of those things you use constantly without thinking about. It's in your JWTs, your email attachments, your data URIs, your Basic Auth headers. And yet a surprising number of developers are fuzzy on what it actually does — which leads to bugs that are annoying to track down.
So let's clear it up.
What Base64 actually is
Base64 is an encoding, not encryption and not compression. It takes arbitrary binary data and represents it using only 64 printable ASCII characters: A–Z, a–z, 0–9, plus + and /. An equals sign = is used for padding.
The point is to move binary data safely through systems that were designed for text. Plenty of protocols and formats choke on raw bytes — a null byte or a high-bit character can get mangled, stripped, or misinterpreted. Base64 sidesteps all of that by sticking to characters everyone agrees on.
The mechanics are simple: it takes three bytes (24 bits) at a time and splits them into four 6-bit groups. Each 6-bit group maps to one of the 64 characters. Three input bytes become four output characters.
The size cost
That 3-to-4 ratio is why Base64 inflates your data by roughly 33%. A 900-byte image becomes about 1,200 characters. That's the price of making binary data text-safe, and it's worth remembering before you inline a 2 MB image as a data URI.
When you'd actually use it
Common, legitimate uses:
- Data URIs — embedding small images, fonts, or SVGs directly in CSS or HTML so the browser doesn't make an extra request.
- Email attachments — MIME uses Base64 because SMTP was built for 7-bit ASCII.
- JWTs — the header and payload are Base64url-encoded JSON (more on that variant below).
- HTTP Basic Auth —
username:passwordgets Base64-encoded into theAuthorizationheader. - Storing or transmitting binary blobs in JSON, XML, or env vars — anywhere the surrounding format only expects text.
And where you should not use it:
- As a security measure. Base64 is trivially reversible. Anyone can decode it in a second. It hides nothing.
- For large files. That 33% overhead adds up, and inlining megabytes into a page or document is almost always a mistake.
Gotchas that'll bite you
Base64 vs Base64url
This is the big one. Standard Base64 uses + and /, but those characters have special meaning in URLs and filenames. So there's a URL-safe variant that swaps them for - and _, and often drops the padding = entirely.
JWTs use Base64url. If you try to decode a JWT segment with a plain Base64 decoder, it may fail or produce garbage because of those substituted characters. Always check which variant you're dealing with.
Character encoding of the original string
Base64 encodes bytes, not text. If you encode the string café, the result depends entirely on whether that string was UTF-8, Latin-1, or something else first. Encode as UTF-8 on one side and decode assuming Latin-1 on the other, and you'll get mojibake. The Base64 round-trip is lossless — the encoding confusion happens at the text-to-bytes boundary.
Whitespace and line wrapping
Some Base64 output (MIME in particular) wraps lines at 76 characters. Some decoders tolerate the newlines, some don't. If a decode fails for no obvious reason, strip the whitespace first.
Padding
The = padding tells the decoder how many bytes were in the final group. Some systems require it, some reject it, some don't care. If you're hand-stitching tokens together, this is a classic source of "works locally, breaks in prod" issues.
A quick worked example
Let's encode the three-character string Man by hand, because seeing it click makes the whole thing make sense.
Characters: M a n
ASCII: 77 97 110
Binary: 01001101 01100001 01101110
Regroup into 6-bit chunks:
010011 010110 000101 101110
Decimal: 19 22 5 46
Base64: T W F u
Result: "TWFu"Three input bytes, four output characters, no padding needed because the input divided evenly. If you'd encoded just Ma (two bytes), you'd get one = of padding; a single byte gives you two.
In practice you won't do this by hand, but it's good to know the output length is predictable: ceil(bytes / 3) * 4 characters.
Doing it in code
Most languages make this painless. In Node.js:
// Encode
const encoded = Buffer.from('Man', 'utf8').toString('base64');
// "TWFu"
// Decode
const decoded = Buffer.from('TWFu', 'base64').toString('utf8');
// "Man"
// Base64url for tokens
const urlSafe = Buffer.from('Man').toString('base64url');In the browser, btoa() and atob() exist but only handle Latin-1 strings, so for Unicode you need to convert through TextEncoder first:
const bytes = new TextEncoder().encode('café');
const b64 = btoa(String.fromCharCode(...bytes));And in Python:
import base64
base64.b64encode(b'Man') # b'TWFu'
base64.urlsafe_b64decode(token) # URL-safe variantWhen you just need a quick decode
Half the time you're not writing code at all — you've got a JWT payload, a data URI, or a Basic Auth header and you want to see what's inside it right now. Spinning up a REPL for that is overkill. I keep a Base64 Encoder / Decoder bookmarked for exactly these moments: paste in the string, read the output, move on. It handles the standard and URL-safe variants, which saves you the "why won't this decode" dance.
The takeaway
Base64 is a transport mechanism for binary data over text channels. That's the whole story. Keep three things in mind and you'll rarely get tripped up:
- It's encoding, not encryption — never rely on it to hide anything.
- Know whether you need the standard or URL-safe variant, especially with tokens.
- Be deliberate about the text encoding of your original data before you encode the bytes.
Get those right and Base64 becomes exactly what it should be: a boring, reliable tool that quietly does its job.
Photo by Markus Spiske on Unsplash