Base64 Explained: Encoding, Not Encryption

How Base64 works, why it is not encryption, when to use the URL-safe variant, and the Data URL mistakes that break large images.

Published November 20, 2026 base64encodingdevelopers

Base64 turns binary data into plain ASCII text. That is all it does. It does not hide anything, it does not compress anything, and it is not a security measure — yet it is routinely described as if it were.

This guide covers what Base64 actually guarantees, why the output is a third larger, and where the URL-safe variant and Data URLs cause trouble.

Why Base64 exists

Many protocols and file formats only accept text. Email bodies were designed for 7-bit ASCII, JSON strings cannot contain raw bytes, and XML cannot carry a JPEG. Base64 solves that by mapping every 3 bytes of input to 4 printable characters.

The alphabet is A–Z, a–z, 0–9, + and /, with = used for padding. All 64 characters are safe in almost any text context, which is why the encoding shows up everywhere: email attachments, JSON Web Tokens, data URLs, Kubernetes secrets and HTTP Basic authentication.

The 33% cost

Every 3 input bytes become 4 output characters. The output is therefore about 4/3 = 133% of the input size, plus padding.

InputBase64 outputOverhead
3 bytes4 characters+33%
1 KB~1.37 KB+37%
1 MB~1.37 MB+37%

That overhead matters. Embedding a 2 MB image as Base64 in a JSON response makes the response roughly 2.7 MB before compression, and the browser still has to decode it before rendering. If you control both ends of the connection, send binary and skip the encoding.

Padding and the = sign

Base64 processes input in groups of 3 bytes. When the last group is short, padding fills the gap:

  • 3 bytes → 4 characters, no padding.
  • 2 bytes → 3 characters plus =.
  • 1 byte → 2 characters plus ==.

Padding is not optional in standard Base64, but many decoders tolerate its absence. If a decoder rejects a string that looks valid, check the length: a valid Base64 string (without padding) has a length that is not congruent to 1 modulo 4.

Standard vs URL-safe Base64

The standard alphabet includes + and /. Both have meaning in URLs and file paths, so a variant replaces them:

StandardURL-safe
+-
/_

JWT, JWK thumbprints and many APIs use the URL-safe variant, usually without padding. A token produced with - and _ will fail in a strict standard decoder, and a standard string containing + or / will break if pasted into a URL without encoding. Always know which variant the other side expects.

Data URLs

A Data URL embeds a file directly in a document:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB...

They are convenient for small icons, CSS backgrounds and single-file demos. They are a poor fit for large assets because:

  • the encoded payload is a third larger;
  • it cannot be cached separately by the browser;
  • it inflates HTML, CSS or JSON that may be parsed on every request;
  • some tools and proxies truncate very long URLs or attribute values.

As a rough rule, inline assets below 2–4 KB and keep everything else as a separate file. The Image to Base64 converter shows the encoded size before you paste it into a stylesheet.

Base64 is not encryption

Encoding changes the representation; encryption changes who can read the data. Base64 has no key, no secrecy and no integrity check. Anyone can decode it, including a curious user, a log collector or an attacker who finds the string in a backup.

This is why these are all bad ideas:

  • storing passwords as Base64;
  • hiding an API key in a Base64 string in client-side JavaScript;
  • assuming a Base64-obfuscated payload is safe to log;
  • using Base64 as a substitute for HTTPS.

If you need confidentiality, use encryption with a managed key. If you need integrity, use a signature or an HMAC. If you need a password store, use a password hash such as Argon2 or bcrypt — see the Hash Generator for quick digests during debugging.

When Base64 is the right tool

  • Embedding a small binary in a text-only format (JSON, XML, YAML).
  • Sending binary over a transport that only handles text.
  • Encoding credentials in HTTP Basic authentication (always over TLS).
  • Representing keys and certificates in configuration files.
  • Carrying binary data inside a JWT or another signed token.

Outside those cases, the encoding usually adds size without adding value.

Common mistakes

  1. Decoding with the wrong alphabet. URL-safe strings need a URL-safe decoder.
  2. Losing padding. Trimming = is fine for transport, but some decoders require it.
  3. Encoding already-encoded data. Double-encoded strings decode to garbage; check the prefix and length before decoding twice.
  4. Assuming the output is URL-safe. A raw Base64 string with + or / must be percent-encoded before it goes in a query parameter.
  5. Comparing secrets as strings. Base64 of a secret is still the secret; compare hashes, not encoded values.
  6. Using it on huge files in memory. Decoding a 500 MB string in the browser will freeze the tab.

A short checklist

  1. Decide whether you need text-safety (Base64) or secrecy (encryption).
  2. Pick the alphabet deliberately: standard or URL-safe.
  3. Keep padding unless the consumer explicitly does not want it.
  4. Avoid inlining large assets as Data URLs.
  5. Never store secrets in an encoded form and call it protected.
  6. Verify a round trip — encode, decode, compare bytes — when integrating a new library.

Base64 is a transport detail, not a security feature. Treat it that way and it stays useful and boring, which is exactly what an encoding should be.

Tools mentioned in this article

Back to the blog