When to Use Base64 Encoding (and When Not To)

By The UtilCraft team · 4 min read ·

Base64 shows up everywhere once you start noticing it: in data URIs, email source, JWTs, Kubernetes secrets, API payloads. It's a simple, useful tool with one job, and most problems with it come from using it for a different job. Here's what it's for, what it costs, and the cases where you should reach for something else.

What Base64 actually solves

Lots of systems were built to carry text: email, JSON, XML, URLs, HTTP headers, YAML files. Binary data, like an image, a cryptographic key or a compressed file, contains byte values that these systems mangle, strip or misinterpret: null bytes, line endings, characters outside ASCII.

Base64 maps every 3 bytes of binary data to 4 characters from a safe alphabet of 64 (A–Z, a–z, 0–9, + and /). The result survives any text channel intact, and can be decoded back to the exact original bytes. That's the whole purpose: moving binary data through places that only accept text.

The cost: a third bigger, and no secrecy

Four characters for every three bytes means Base64 output is 33% largerthan the input, plus a little padding. A 750 KB image becomes 1 MB of text. If line breaks are added every 76 characters, as MIME email does, it's slightly more.

And it offers zero confidentiality. There's no key; anyone can decode it, instantly, with tools built into every operating system. Seeing cGFzc3dvcmQxMjM=in a config file should register as "password123", because that's exactly what it is.

Good uses

Email attachments

The original use case, and still the main one. The SMTP protocol was designed for 7-bit text, so MIME encodes attachments as Base64. Your email client handles it invisibly.

Small inline images and fonts

A data URI like data:image/svg+xml;base64,…embeds an asset directly in HTML or CSS, saving an HTTP request. That made a real difference before HTTP/2. Today it's still sensible for tiny assets, say under 2–4 KB, like icons or a small placeholder image shown while the real one loads.

Binary values inside JSON

JSON has no binary type, so hashes, signatures, encryption nonces and small files are usually sent as Base64 strings. The same goes for JWTs, whose three parts are Base64URL-encoded JSON and a binary signature.

Basic authentication and config values

The HTTP Authorization: Basic header carries username:password as Base64, and Kubernetes stores Secret values that way. In both cases, Base64 exists so arbitrary bytes fit in a header or a YAML file, not to protect anything. The protection has to come from HTTPS and from access controls on the cluster.

Bad uses

"Encrypting" or hiding data

This is the big one. Base64-encoding a password, an API key or personal data and calling it protected is a mistake auditors find regularly. If it needs to be secret, encrypt it with a real algorithm and manage the key properly. If it needs to be verified but not recovered, as with passwords, hash it with a slow password-hashing function.

Inlining large images

Embedding a 200 KB photo as a data URI makes it 267 KB, blocks the HTML or CSS from finishing parsing until it's all downloaded, can't be cached separately from the page, and can't be lazy-loaded. A normal image file served over HTTP/2 or HTTP/3 is better in every way.

Uploading files through JSON APIs

Sending a 20 MB video as a Base64 string inside JSON means 27 MB on the wire, plus the whole thing held in memory as a string on both ends. Use a multipart/form-data upload or a direct upload to object storage instead.

Storing binary data in a database

Most databases have a binary column type (bytea, BLOB, VARBINARY). Storing Base64 text instead costs a third more space and an encode/decode on every access.

Base64 vs Base64URL vs URL encoding

PurposeExample
Base64Binary → texta+b/c==
Base64URLBinary → text safe in URLs and file namesa-b_c
URL (percent) encodingText → text safe in a URLa%20b%26c

Base64URL swaps + and /, which have special meanings in URLs, for - and _, and usually drops the padding. It's what JWTs use. URL encoding is a different tool entirely: it escapes the few characters in a piece of text that would break a URL, and leaves the rest readable.

Quick reference

  • Encode or decode in the browser: our Base64 Encoder / Decoder handles text (including emoji and accents) and files, with a URL-safe option.
  • Escape text for a URL: the URL Encoder.
  • Terminal: base64 file.png to encode, base64 --decode to decode. Use echo -n so the trailing newline isn't encoded too.

Tools used in this guide

Keep reading