Toolwise
Developer Tools

Base64 Encoding Explained (And Why It Isn't Encryption)

By the GrainZap Team8 min read
base64encodingdeveloper toolsdata uri
Advertisement

Base64 shows up everywhere in software: in email attachments, in data URIs embedded in CSS, in JWTs, in API payloads carrying file uploads, in Kubernetes secrets. It's so common that many developers use it daily without ever being told what it actually does — which is how it ends up misused as a security measure, or blamed for making payloads mysteriously larger.

The concept itself is simple. Base64 is a way of representing arbitrary binary data using only 64 printable characters that survive text-only transmission unharmed. It's a translation, not a transformation: nothing is hidden, nothing is compressed, and the original bytes come back exactly as they went in.

What Base64 actually does

Computers store everything as bytes, and a byte can hold any of 256 values. Many of those values aren't printable text — some are control characters, some are interpreted as line endings, some get mangled by systems that assume 7-bit ASCII. If you try to paste raw binary into a JSON string or an email body, something in the chain will corrupt it.

Base64 solves this by re-grouping the bits. It takes three bytes — 24 bits — and splits them into four groups of 6 bits each. Six bits can represent 64 values, and each of those maps to one character from a fixed alphabet: A–Z, a–z, 0–9, plus + and /. So three arbitrary bytes become four characters that every text system handles without complaint.

  • Input: 3 bytes (24 bits) of any value at all, including bytes that aren't valid text.
  • Regrouping: those 24 bits are re-split into 4 chunks of 6 bits.
  • Lookup: each 6-bit chunk (a value from 0 to 63) indexes into the 64-character alphabet.
  • Output: 4 printable ASCII characters, safe to put in JSON, XML, email or a URL query.
  • Reversing it needs nothing but the same alphabet — no key, no password, no configuration.

Padding, and why strings end with =

The 3-bytes-to-4-characters mapping is clean only when your input length is a multiple of three, which real data rarely is. When there's a leftover group of one or two bytes, Base64 pads the final block with = characters so the output length stays a multiple of four.

  • Input length divisible by 3 → no padding, output ends with a normal character.
  • One leftover byte (8 bits) → encodes to 2 characters plus two = signs.
  • Two leftover bytes (16 bits) → encodes to 3 characters plus one = sign.
  • "Ma" encodes to "TWE=", while "Man" encodes to "TWFu" with no padding at all.

The = characters carry no data. They exist so a decoder reading a stream in fixed 4-character blocks knows where the real data ended. Some decoders accept unpadded input and some reject it, which is why padding occasionally causes an interoperability bug between two systems that both claim to "support Base64".

Where Base64 genuinely earns its place

Base64 isn't used because it's clever — it's used because a lot of infrastructure is text-only, and binary data has to get through it somehow.

Text-only data formats

JSON and XML are text formats. A JSON string can't legally contain arbitrary bytes, so an API that accepts a file upload inside a JSON body has to encode that file as text first, and Base64 is the standard choice. The same applies to storing a binary blob in a YAML config or an environment variable.

Email and MIME

Email was designed around 7-bit ASCII, long before anyone attached a photo to a message. MIME added Content-Transfer-Encoding: base64, which is how every image, PDF and spreadsheet you've ever emailed actually travels — encoded into printable characters, then decoded by the recipient's client.

Data URIs in CSS and HTML

A data URI embeds a file directly in a document instead of referencing a separate URL, using a form like data:image/png;base64,iVBORw0KGgo... This eliminates an HTTP request, which can be worth it for a tiny icon or an SVG used once. It's a genuine trade-off though: embedded assets can't be cached independently, they inflate the parent file, and they bloat every page load rather than being fetched once. Reserve it for assets small enough that the saved request outweighs the extra bytes.

What Base64 is not

Two persistent misconceptions cause real problems in production systems, and both are worth being blunt about.

It is not encryption

There is no key involved anywhere in Base64. Anyone holding an encoded string can decode it instantly with a browser tool, a one-line shell command, or thirty seconds of mental effort if they recognise the alphabet. Encoding a password, an API key or a customer record in Base64 provides exactly zero protection — it only makes the value slightly less obvious to a human scanning a file.

Watch out

Kubernetes secrets are stored Base64-encoded by default, and a lot of teams read that as "encrypted". It isn't — it's encoding, applied so binary values can live in YAML. Anything genuinely sensitive needs encryption at rest, a secrets manager, or both.

It is not compression

Base64 makes data bigger, not smaller. Every 3 bytes become 4 characters, an increase of about 33%, plus padding and any line breaks the format inserts. A 1 MB image becomes roughly 1.37 MB of Base64 text. That overhead is the price of text-safety, and it's the main argument against inlining large assets as data URIs.

Base64 versus base64url

Standard Base64's alphabet includes + and /, and its padding uses =. All three characters have special meaning in URLs: / is a path separator, + is sometimes read as a space in query strings, and = separates keys from values. Putting standard Base64 in a URL means percent-encoding it, which makes the string longer and harder to read.

RFC 4648 therefore defines a URL-safe variant, base64url, with two substitutions and one convention:

  • + becomes - (hyphen).
  • / becomes _ (underscore).
  • = padding is usually omitted entirely, since the length can be inferred.
  • Everything else — the alphabet's first 62 characters and the 3-to-4 byte mapping — is identical.

This is the variant JWTs use for all three of their segments, which is why you can drop a token into a URL or header without escaping it. It's also why pasting a JWT segment into a strict standard-Base64 decoder sometimes fails: the decoder chokes on - and _, or refuses the missing padding.

Tip

If a Base64 string won't decode, check three things in order: whether it's actually base64url (look for - or _), whether padding was stripped, and whether copying it introduced line breaks or spaces. Those three account for nearly every "invalid Base64" error in practice.

Frequently asked questions

Should I Base64-encode data before compressing it?

No — compress first, then encode if you need to. Base64 output has less redundancy relative to its size than the original binary, so compressing afterwards is less effective and you've already paid the 33% expansion. The correct order is compress, then encode; on the way back, decode, then decompress.

How do I Base64-encode text with emoji or accented characters?

Base64 operates on bytes, not characters, so the text has to be converted to bytes first — normally with UTF-8. A single emoji is four UTF-8 bytes, so it produces noticeably more Base64 output than a single ASCII letter. In JavaScript, this is why btoa() throws on non-Latin-1 strings and you need to run the text through a TextEncoder first.

Is there a difference between Base64 and hex encoding?

Both make binary data printable, but hex uses 16 characters and produces exactly two characters per byte — a 100% size increase, versus Base64's 33%. Hex is easier to read and align by eye, which is why hashes and memory dumps use it, while Base64 is preferred where size matters more than human readability.

The short version: Base64 is a byte-to-text translation with a fixed public alphabet, it grows your data by a third, and it keeps no secrets. Use it to move binary through text-only channels, prefer base64url anywhere a URL is involved, and never let it stand in for encryption.

Advertisement

Ready to put this into practice?

Encode Base64 Free

More guides