Toolbox Hub
Developer Tools11 min read

Base64 Is Not Encryption: A Field Guide to What the Encoder Actually Does

Why Base64 is reversible by anyone, why Unicode breaks btoa(), where the 33 percent size increase comes from, and when inlining an image as a data URI is a mistake.

What this guide covers

  • 1. What the transformation actually does
  • 2. Why 'reversible by anyone' matters in practice
  • 3. The Unicode wall: why btoa() throws
  • 4. Three characters that break URLs
  • 5. When inlining an image is a mistake
  • 6. A repeatable way to diagnose a mystery Base64 string
  • 7. Frequently asked questions
  • 8. The one-line version

Base64 shows up in certificate files, HTTP headers, data URIs, JSON payloads and debugging logs. It also shows up in security reviews, where somebody has used it to hide a value and is surprised to learn they have hidden nothing at all.

The confusion is understandable: encoded output looks scrambled. But scrambled is not the same as protected. This guide is about what the transformation actually does, where its edges are, and the specific failure modes you will hit in a browser — including one that only appears when your text contains a character outside Latin-1.

What the transformation actually does

Base64 takes a stream of bytes and rewrites it using 64 printable ASCII characters: A–Z, a–z, 0–9, plus two more symbols and a padding character. It works in groups of three input bytes (24 bits), splitting them into four 6-bit chunks, each of which maps to one character in that alphabet. Three bytes in, four characters out.

That four-for-three ratio is the whole explanation for the size increase. Encoded data is almost exactly one third larger than the original, before you account for line wrapping. A 300 KB image becomes roughly 400 KB of text. Nothing is compressed, deduplicated or optimised; the operation is purely representational, and it is completely reversible.

  • No key is involved. There is no secret, no password and no parameter that changes the output. Two people encoding the same bytes get the same string.
  • It is not a hash. A hash is one-way and fixed length; Base64 preserves every input bit and can be undone by anyone. If you need to verify that two files match, use the MD5 / SHA Hash Generator instead.
  • It is not compression. It makes data bigger, always. Its only job is to make binary data safe to carry through a channel that expects text.

Why 'reversible by anyone' matters in practice

The distinction between encoding, hashing and encryption is not academic. Encoding answers 'how do I carry these bytes through a text-only channel'. Hashing answers 'are these two things the same, without storing the thing'. Encryption answers 'who is allowed to read this'. Only the third has an access control story.

A Base64 string in a URL, a cookie or a log file is plaintext with extra steps. `dXNlcjpwYXNzd29yZA==` decodes to `user:password` in any decoder, including the Base64 Encoder / Decoder on this site. HTTP Basic authentication uses exactly this pattern and is safe only because TLS protects the transport, not because the header is unreadable. If you ever see Base64 described as a way to protect a secret, the correct response is to ask where the key is. There isn't one.

The Unicode wall: why btoa() throws

Here is the browser-specific part that catches people. The built-in `btoa()` function does not accept a JavaScript string in the general sense. It accepts a binary string in which every character code is between 0 and 255 — effectively Latin-1. Pass it 'hello' and it works. Pass it '你好' or an emoji and it throws `InvalidCharacterError: The string to be encoded contains characters outside of the Latin1 range`.

This is not a bug in `btoa`. JavaScript strings are UTF-16, so a single é, 中 or 🙂 is a code unit (or a surrogate pair) whose numeric value is far above 255. Base64 operates on bytes, and the browser is refusing to guess how you want your characters turned into bytes.

The encoder on this site handles it with a three-step bridge: `btoa(unescape(encodeURIComponent(input)))` for encoding, and `decodeURIComponent(escape(atob(input)))` for decoding. `encodeURIComponent` first produces UTF-8 percent-encoded bytes; `unescape` turns each `%XX` into a single character whose code equals that byte; `btoa` then encodes those bytes. Decoding reverses each step. `escape` and `unescape` are deprecated legacy APIs, but they remain the shortest reliable byte-string bridge in a browser; a modern equivalent uses `TextEncoder` and builds a binary string from the resulting `Uint8Array`.

There is a second, quieter edge: because the encoder catches any exception and reports `Invalid Base64 string.` on decode, a whitespace-corrupted string and a genuinely wrong string produce the same message. Base64 copied out of a PDF, an email with hard line wraps, or a wrapped terminal often contains newlines or spaces. Some decoders tolerate them and some do not. Strip whitespace before decoding and check that the remaining length is a multiple of four; that resolves a large share of 'invalid' reports.

Three characters that break URLs

The standard Base64 alphabet includes `+`, `/` and `=`. All three are meaningful in URLs. `+` in a query string is commonly interpreted as a space by form decoders, `/` changes path segmentation, and `=` collides with parameter separators. A token that decoded perfectly a moment ago can arrive at your server subtly changed.

  • URL-safe variant: replace `+` with `-`, `/` with `_`, and usually drop the `=` padding. This is the form used by JWTs and many APIs. It is not what a standard encoder emits, so convert deliberately.
  • Or percent-encode it: running the string through the URL Encoder / Decoder keeps the standard alphabet intact and makes the intent explicit. This is the safer choice when you do not control the receiver.
  • Never assume the receiver normalises: if a value crosses a boundary you do not own, write down which variant you used. Most 'token works locally, fails in production' reports are this, not a signing problem.

When inlining an image is a mistake

A data URI such as `data:image/png;base64,...` removes an HTTP request, which sounds like a free performance win. Three costs usually outweigh it: the payload is about 33 percent larger, it cannot be cached independently of the document, and it is re-downloaded every time the HTML changes. Inlining a 2 KB icon used once in a single email template is reasonable. Inlining a 40 KB logo used across a site is a regression.

  • Good fit: a tiny icon under a few kilobytes, used once, on a page where an extra round trip genuinely hurts.
  • Bad fit: anything repeated across pages, anything the browser should cache, anything large enough to delay HTML parsing.
  • Check the trade: the Image to Base64 converter gives you the string; the File Size Converter makes the decimal-versus-binary size difference explicit before you argue about a budget.

A repeatable way to diagnose a mystery Base64 string

When an unfamiliar encoded value turns up in a log, resist the urge to paste it into a search engine. Work through it in order — each step rules out a class of causes.

  • 1. Inspect the shape. Strip whitespace, check the length is a multiple of four, look for trailing `=` padding, and note whether `-` or `_` appear (URL-safe variant).
  • 2. Decode once. Use the Base64 Encoder / Decoder. If it fails, suspect whitespace or a truncated copy before suspecting the data.
  • 3. Decide what you are looking at. Readable UTF-8 text is one thing; arbitrary bytes are another. Feed the result to the Hex to Text converter when the output is not printable — a PNG begins with a recognisable byte signature, and a gzip stream has one too.
  • 4. Only then interpret. A JWT has three dot-separated segments. A PEM block has header and footer lines. A Basic auth header decodes to `user:password`. Identifying the container first stops you from misreading the contents.

Frequently asked questions

Is Base64 secure for storing passwords in a config file? No. It is plaintext. Anyone with the file has the value.

Why is my encoded string longer than the file? Because three bytes become four characters. That is expected, not a failed compression.

Why does encoding Chinese text fail in some tools? Those tools call `btoa()` directly on a UTF-16 string. The encoder here bridges through UTF-8 first, so it handles non-ASCII input.

Can I safely put the output in a URL? Not without either switching to the URL-safe alphabet or percent-encoding it. `+`, `/` and `=` are the problem characters.

Does decoding ever lose data? No, if the string is intact. Base64 is a bijective mapping over bytes. What it can lose is the *interpretation* of those bytes — whether they were UTF-8, Latin-1 or a binary format — and that information never travelled with the string.

The one-line version

Base64 is a transport format for bytes, not a security control. It grows data by a third, needs a UTF-8 bridge for non-ASCII text, needs care around URLs, and always decodes back to exactly what went in — which is precisely why you should never put anything secret into it.

Tools run privately in your browser

Every tool referenced in this guide processes your input in the browser rather than uploading it to a server we operate, and requires no account. A typical single job stays free. Larger batches, history export, and an ad-free page are optional on Pro. A few tools load a processing engine from a CDN on first use; see the privacy policy for details. Browse the full toolbox →

More from the blog