Base64 is one of those things you've used a hundred times without thinking about it. It's in your JWTs, your email attachments, your data URIs, your API payloads. And yet a surprising number of developers carry around a wrong mental model of what it's for. Let's fix that.
What base64 actually is
Base64 is an encoding scheme that represents arbitrary binary data using only 64 printable ASCII characters: A–Z, a–z, 0–9, plus + and /. The = character is used for padding.
The mechanism is simple. It takes three bytes (24 bits) of input and splits them into four groups of 6 bits. Each 6-bit group maps to one character in the alphabet, because 2^6 = 64. That's the whole trick.
The important consequence: base64 inflates your data by roughly 33%. Three bytes in, four characters out. If you base64-encode a 1 MB file, you get about 1.33 MB of text. Keep that in mind before you start stuffing images into JSON.
What base64 is NOT
This is the big one. Base64 is not encryption. It provides zero security. Anyone can decode it instantly — there's no key, no secret, nothing. If you're "hiding" a password or token by base64-encoding it, you've hidden nothing.
It's also not compression. In fact it does the opposite, making data larger. And it's not hashing — it's fully reversible by design, which is the entire point.
Base64 has exactly one job: safely carrying binary data through channels that only reliably handle text.
When you'd actually use it
The common thread is always a text-only transport. Some real cases:
- Email attachments. SMTP was built for 7-bit ASCII. MIME uses base64 to shove binary files through it.
- Data URIs. Embedding a small image directly in CSS or HTML with
data:image/png;base64,...to avoid an extra HTTP request. - JSON and XML payloads. These formats are text, so binary blobs (a thumbnail, a signed certificate) get base64-encoded to travel inside them.
- JWTs. The header and payload are base64url-encoded JSON. The signature is base64url-encoded bytes.
- Basic Auth headers.
Authorization: Basicfollowed by base64 ofusername:password. Note: this is why Basic Auth over plain HTTP is unsafe — base64 is trivially reversible. - Binary data in environment variables or config files. A TLS key or a service-account JSON sometimes gets base64-encoded so it survives being pasted into a CI secret field.
The gotchas that bite people
Standard vs URL-safe
Standard base64 uses + and /. Both of those are special in URLs and filenames. So there's a variant called base64url that swaps them for - and _, and often drops the padding entirely. JWTs use base64url. If you decode a JWT with a standard decoder and it chokes, this is usually why.
Padding
The = padding exists so decoders know how many bytes the final group represents. Some implementations require it, some don't, and base64url frequently omits it. A string ending in one or two = is normal — it's not corruption.
Encoding of the source text matters
Base64 operates on bytes, not characters. If your input is a string containing non-ASCII characters, you must decide on a text encoding (almost always UTF-8) before base64 kicks in. Skip this step and you'll get mangled accents and emoji on the other end. Encode to UTF-8 bytes, then base64 those bytes.
Line wrapping
Classic MIME base64 wraps output at 76 characters with line breaks. Modern APIs usually produce one long unbroken string. If you feed a wrapped string into a strict decoder, those newlines can cause failures. Strip whitespace first when in doubt.
A worked example
Say you want to encode the string Hello!. In UTF-8 those six bytes are:
H e l l o !
72 101 108 108 111 33 (decimal)
01001000 01100101 01101100 01101100 01101111 00100001 (binary)Group the bits into sixes instead of eights:
010010 000110 010101 101100 011011 000110 111100 100001
18 6 21 44 27 6 60 33Map each number to the base64 alphabet (0=A, 1=B, ... 25=Z, 26=a, ...):
18=S 6=G 21=V 44=s 27=b 6=G 60=8 33=h
=> SGVsbG8hSo Hello! becomes SGVsbG8h. Six input bytes is a clean multiple of three, so no padding is needed here.
In code, you rarely do this by hand. Most languages have it built in:
// JavaScript (browser / Node)
btoa('Hello!'); // 'SGVsbG8h'
atob('SGVsbG8h'); // 'Hello!'
// Node, handling arbitrary bytes properly
Buffer.from('Hello!', 'utf8').toString('base64'); // 'SGVsbG8h'
Buffer.from('SGVsbG8h', 'base64').toString('utf8'); // 'Hello!'One caveat in the browser: btoa throws on characters outside the Latin-1 range. For Unicode text you need to convert to UTF-8 bytes first, for example with TextEncoder, then base64 those. This is a frequent source of "InvalidCharacterError" headaches.
When I just need to eyeball a value, decode a JWT payload, or sanity-check what a service returned, I reach for a quick web tool rather than opening a REPL. Our Base64 Encoder / Decoder handles both directions and the URL-safe variant, which covers most of these day-to-day checks.
A few practical rules of thumb
- Use base64 only when you genuinely have a text-only channel. If the transport handles binary, send binary.
- Never treat base64 as a security measure. For secrets, use actual encryption.
- Pick UTF-8 as your byte encoding and be consistent on both ends.
- Know whether your context wants standard or URL-safe, and whether padding is expected.
- Remember the 33% size overhead before embedding large assets as data URIs.
Base64 is boring in the best way. It does one small job reliably, and once you understand the byte-to-character mapping, the quirks around padding and the URL-safe variant stop being mysterious and start being obvious.
Photo by Jake Walker on Unsplash