Base64 shows up everywhere once you start looking for it: data URIs in CSS, JWT headers, email attachments, Basic Auth headers, API payloads that carry binary blobs. Most developers use it without ever really understanding what it does — and that's usually fine, right up until it isn't.

What Base64 actually is

Base64 is an encoding scheme, not encryption. It takes arbitrary binary data and represents it using only 64 printable ASCII characters: A-Z, a-z, 0-9, plus + and /, with = used for padding. That's it. There is no key, no secret, nothing hidden. Anyone can decode it instantly.

The mechanism is simple. It reads your input three bytes at a time — that's 24 bits — and splits those 24 bits into four 6-bit groups. Each 6-bit value (0 to 63) maps to one character in the alphabet. Three bytes in, four characters out. That's why Base64 output is always about 33% larger than the original.

When the input isn't a clean multiple of three bytes, the encoder pads the final group with = signs so the output length is always a multiple of four.

When you'd actually use it

The core use case is moving binary data through a channel that only reliably handles text. Some practical examples:

  • Data URIs — embedding a small image or font directly in HTML/CSS so the browser doesn't make a separate request.
  • Email (MIME) — SMTP was designed for 7-bit text, so attachments get Base64-encoded.
  • HTTP Basic Auth — the Authorization header carries base64(username:password).
  • JWTs — the header and payload segments are Base64url-encoded JSON.
  • Config and env vars — stashing a certificate or binary secret into a single-line string.

The theme is consistency: text-safe, no newlines mangling your data, no encoding negotiation. You trade a bit of size for a lot of reliability.

When you should NOT use it

Please don't treat Base64 as security. I've seen "obfuscated" API keys shipped in a frontend bundle as Base64 strings, as if that stops anyone. It takes one line of code to reverse. If something needs to be secret, encrypt it.

Also think twice before Base64-encoding large payloads. That 33% overhead adds up fast over the wire, and you're spending CPU to encode and decode on both ends. For big binary transfers, a raw binary channel or multipart upload is almost always better.

A quick worked example

Let's encode the word Man by hand, because seeing the bits helps it stick. The ASCII codes are M=77, a=97, n=110.

M        a        n
01001101 01100001 01101110   (24 bits)

Regroup into 6-bit chunks:
010011 010110 000101 101110
   19     22      5     46

Map to the alphabet:
   19=T  22=W  5=F  46=u

Result: "TWFu"

Now a short case with padding. Encoding just M (one byte, 8 bits) leaves you short of a full 24-bit group, so you get two Base64 characters plus two = pad characters: TQ==. One leftover byte gives two pads; two leftover bytes give one pad.

In real code

Every language has this built in. A few you'll reach for:

// JavaScript (Node.js)
Buffer.from('Man').toString('base64');          // 'TWFu'
Buffer.from('TWFu', 'base64').toString('utf8');  // 'Man'

// Python
import base64
base64.b64encode(b'Man')            # b'TWFu'
base64.b64decode('TWFu')            # b'Man'

// Browser (careful — see gotchas)
btoa('Man');   // 'TWFu'
atob('TWFu');  // 'Man'

When I just need to eyeball or debug a value during development, I'll often paste it into an online tool rather than drop into a REPL. Devsly's Base64 Encoder / Decoder handles that quickly, and it's handy for decoding a JWT payload or inspecting a data URI you copied from somewhere.

The gotchas that actually bite

btoa/atob and Unicode

The browser's btoa only works on strings where every character is in the Latin-1 range. Feed it an emoji or a non-Latin character and you'll get a InvalidCharacterError. The fix is to encode to UTF-8 bytes first:

// Safe for Unicode in the browser
const encoded = btoa(
  String.fromCharCode(...new TextEncoder().encode('Héllo 👋'))
);

Base64 vs Base64url

Standard Base64 uses + and /, which are not URL-safe and definitely not filename-safe. The URL-safe variant swaps them for - and _ and often drops the padding. JWTs use Base64url. If you decode a token with a standard decoder and it chokes on a -, that's why. Always match the variant.

Line breaks

Some encoders (classic MIME, older OpenSSL output) insert a newline every 64 or 76 characters. Other decoders don't expect those and fail. If a decode mysteriously breaks, strip whitespace first.

Padding strictness

Some decoders demand correct = padding; others are lenient. If you strip padding to save bytes, make sure the receiving end can handle it — or re-add it before decoding.

It's not a checksum

Base64 does nothing to verify integrity. A corrupted Base64 string can still decode to garbage without error. If you need to know the data arrived intact, use a hash alongside it.

The short version

Base64 is a reliable way to stuff binary into a text-shaped hole. Reach for it when a transport only speaks text, keep an eye on the size overhead, pick the right variant for URLs, and never mistake it for security. Once you understand the three-bytes-to-four-characters mechanic, the padding and the gotchas all make sense — and the bugs get a lot easier to spot.

Photo by Jake Walker on Unsplash