Base64 explained: what it is and when to use it
Base64 is not encryption, compression or a secret code — it is a way of writing raw bytes using only 64 printable ASCII characters. It exists because many channels can carry text but not arbitrary bytes: an email body, a JSON string, a URL, or an attribute in old HTML. Whenever you must squeeze binary data into one of those channels, Base64 is the standard way to do it.
What the encoding actually does
Bytes are eight bits, but the 64 Base64 characters only need six bits each. The encoder reads
the input three bytes at a time (24 bits) and splits those 24 bits into four groups of six,
then maps each six-bit group to one of the 64 alphabet characters (A–Z a–z 0–9 + /). If fewer than three bytes are left at the end, it pads the result with one or two = characters so the output stays a multiple of four.
So the string Tesserae is not "encrypted" by this — it is re-expressed. Decoding runs
the same mapping in reverse: skip the padding, turn the letters back into six-bit values and stitch
them back into bytes.
The ~33% size overhead
Because three input bytes become four output characters, Base64 always makes things bigger: every three bytes become four characters, so the size grows by 4⁄3, or about 33%. A 3 MB file becomes roughly 4 MB of text. Gzip can reclaim much of that for compressible input, but for already-compressed data (a JPEG, a PNG, a ZIP) the overhead stays close to the full third.
That growth is why you should think before Base64-ing a binary blob "for storage". If the destination actually accepts bytes — a file system, an HTTP body — send the bytes. Base64 is for text-only channels, not as a general-purpose container.
When to reach for it
- Data URIs in HTML or CSS (
data:image/png;base64,…) so a small asset ships inside the document instead of as a separate request. - Email attachments, where the transport is plain text.
- Embedding a binary payload in JSON — JSON has no byte type, so Base64 is the agreed convention.
- JWTs and API tokens, which are Base64url text on the wire (see below).
The URL-safe variant
The standard alphabet includes + and /, which mean "space" and path
separator in URLs and filenames, so the RFC 4648 §5 alphabet swaps them for - and
_ and drops the trailing = padding. That variant is called base64url and
is what you see inside a JSON Web Token: the three dot-separated segments of a JWT are all unpadded
base64url. The safe rule is: if the value might appear in a URL, a query string or a filename, use
the URL-safe alphabet and no padding.
Unicode caveat
Base64 operates on bytes, so text must be turned into bytes first — normally UTF-8. The naive
browser pair btoa/atob throws on anything outside Latin-1; the correct
path is to UTF-8-encode first. This is exactly why a quick online encoder returns garbage for emoji,
while a proper one round-trips them.
Put it into practice
The Base64 Encode / Decode tool encodes text or a whole file, supports the URL-safe alphabet and 76-character MIME wrapping, and round-trips UTF-8 correctly. For the related encodings, try Base32, Hex or the URL encoder. And if you are hashing rather than encoding, the Which hash should I use? guide covers the difference.
Rules of thumb
- Use it for text-only channels, not as a general container for binary data.
- Never treat it as privacy — an encoded value is one paste into a decoder away from readable.
- Pick the URL-safe alphabet whenever the value might land in a URL or a filename.
- Prefer hex for anything a human will read or diff, such as digests and checksums: it is case-insensitive and contains no punctuation.
- When decoding a pasted value, tolerate stray whitespace and missing padding on input, but be strict when turning a file back into bytes.
More guides
- Which hash should I use? — digests, password storage and HMAC.
- Cron syntax cheat sheet — scheduling recurring jobs without guessing.
- Colour contrast and WCAG — accessible colour choices.