Base64 is one of those things every developer runs into but few stop to understand. You see it in JWTs, email attachments, API payloads, and those long data:image/png;base64,... strings in CSS. People sometimes treat it like encryption or compression. It's neither. Let's clear that up.
What Base64 actually is
Base64 is a way to represent 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 takes three bytes (24 bits) at a time and splits them into four 6-bit groups. Each 6-bit group maps 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. If the input isn't a multiple of three bytes, padding (=) fills the gap.
Crucially: Base64 is not encryption. There's no key, and anyone can decode it instantly. It's not compression either — it makes data bigger, not smaller. It's purely an encoding for safe transport.
When you'd actually use it
The core use case is any time you need to stuff binary data into a text-only channel. A few concrete examples:
- Data URIs — embedding a small image or font directly in HTML/CSS so the browser doesn't make a separate request.
- Email attachments — MIME uses Base64 because SMTP was designed for 7-bit ASCII text.
- JSON payloads — JSON has no binary type, so if you need to send a file or raw bytes in an API request, Base64 is the usual workaround.
- JWTs — the header and payload are Base64url-encoded JSON. (Note: encoded, not encrypted — the contents are readable by anyone.)
- Basic auth — the
Authorization: Basicheader is justbase64(username:password).
That last one is worth dwelling on. Because Basic auth is just Base64, it offers zero confidentiality on its own. Over HTTP it's as good as sending credentials in plain text. Always pair it with HTTPS.
A quick worked example
Say you want to encode the string Hi!. In ASCII those bytes are 72, 105, 33. 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 (index 18 = S, 6 = G, 36 = k, 33 = h):
Hi! -> SGkhNo padding needed here because three bytes divided evenly. If you'd encoded just Hi (two bytes), you'd get SGk= with one padding character.
Doing it in code
Every language has this built in. You rarely need to implement the algorithm yourself:
// JavaScript (Node)
const encoded = Buffer.from('Hi!').toString('base64'); // 'SGkh'
const decoded = Buffer.from(encoded, 'base64').toString(); // 'Hi!'
// Browser
const enc = btoa('Hi!'); // 'SGkh'
const dec = atob('SGkh'); // 'Hi!'# Python
import base64
encoded = base64.b64encode(b'Hi!') # b'SGkh'
decoded = base64.b64decode(encoded) # b'Hi!'When you're debugging and just want to decode a token or check what a blob contains, don't bother writing a script. Paste it into a Base64 Encoder / Decoder and read it instantly. It's the fastest way to peek at a JWT payload or verify an encoded value without leaving your browser.
The gotchas
Base64 vs Base64url
The standard alphabet uses + and /, which are problematic in URLs and filenames. The URL-safe variant swaps them for - and _, and often drops padding. JWTs use Base64url. If you decode a JWT with a plain Base64 decoder and it chokes, mismatched alphabets are usually why. Make sure both ends agree on the variant.
The UTF-8 trap in browsers
btoa() and atob() only handle Latin-1 characters. Feed them an emoji or an accented character and you'll get a DOMException:
btoa('café'); // throws: InvalidCharacterErrorThe fix is to encode to UTF-8 bytes first:
const bytes = new TextEncoder().encode('café');
const b64 = btoa(String.fromCharCode(...bytes));Modern runtimes are adding cleaner APIs, but this footgun has cost a lot of people an afternoon.
Size inflation
Remember the 33% overhead. Embedding a 100KB image as a data URI means shipping ~133KB of text. For small assets that saves a request and can be a win. For anything large, you're bloating your HTML, hurting caching, and slowing first paint. Use data URIs for tiny icons, not hero images.
Line wrapping
Some older encoders (and MIME) insert line breaks every 76 characters. If your decoder doesn't expect them, decoding fails. Most modern libraries handle both, but it's worth knowing if you're parsing output from a legacy system.
Don't treat it as security
I'll say it again because it keeps coming up in code reviews: Base64 is obfuscation at best, and barely that. If you find config files with Base64-"encrypted" passwords, treat them as plain text, because that's what they effectively are.
The mental model to keep
Think of Base64 as a translator, not a lock. It takes bytes that might upset a text-only pipe and rewrites them in a safe dialect that any system can carry. Nothing is hidden, nothing is squeezed smaller, nothing is protected. It's the right tool when the problem is "this channel only handles text and I have binary data." It's the wrong tool for everything else.
Know your variant, watch the Unicode edge cases in the browser, and keep an eye on payload size. Get those three right and Base64 will quietly do its job without ever becoming the thing you're debugging at 2am.
Photo by Markus Spiske on Unsplash