Skip to content
GigAI Tools
developer-tools

Base64 Encoding Explained: What It Is and When to Use It

What Base64 actually does, why it exists, how the encoding works, and the right times to use it, including data URIs, email attachments and embedding files.

Chandrabhan Shekhawat6 mins read
Base64 Encoding Explained: What It Is and When to Use It

Base64 turns up in the strangest places: a giant string of letters where you expected an image, an email header full of gibberish, a config value that looks like line noise. It's one of those concepts that's simpler than it appears once someone explains why it exists. The short version: Base64 lets you carry binary data (images, files, anything) through channels that were only ever built to handle plain text. This guide explains what it is, how it works, when to use it, and just as importantly, when not to.

The problem Base64 solves

Computers store everything as bytes, and a byte can be any of 256 values. But a lot of the internet's plumbing (email, URLs, older protocols, certain config formats) was designed to move text, not arbitrary bytes. Send raw binary through those channels and things break: a byte that happens to match a control character, a line-ending, or a delimiter can corrupt the whole message.

Base64 is the fix. It re-encodes any binary data using only a safe, 64-character alphabet, A–Z, a–z, 0–9, plus + and /. That survives transit through text-only systems untouched. It's not encryption and it's not compression. It's a transport encoding. The goal is safe passage, nothing more.

How the encoding works

The mechanism is elegant. Base64 takes your data three bytes at a time. Three bytes is 24 bits. It re-slices those 24 bits into four groups of 6 bits each. Since 6 bits can represent 64 values (2⁶ = 64), each group maps neatly to one character in the 64-character alphabet.

So every 3 bytes of input become 4 characters of output. That ratio is why Base64 is always about 33% larger than the original: you're spending 4 characters to represent what took 3 bytes. It's the price of text-safety.

When the input isn't an exact multiple of three bytes, Base64 pads the end with one or two = signs. That's why encoded strings so often end in = or ==, the padding tells a decoder how many bytes the final group really held.

Here's the classic tiny example. The text Man (3 bytes) encodes to TWFu (4 characters). The single character M encodes to TQ==, with two padding characters because there was only one byte in the final group.

The URL-safe variant

Two characters in the standard alphabet, + and /, cause trouble inside URLs, where / is a path separator and + can mean a space. So there's a URL-safe Base64 variant that swaps them for - and _, and often drops the = padding. If you're putting encoded data into a query string or a path (as JWTs do) this is the variant you want. It's the same idea, just with two substitutions that keep the result clean in a web address.

When you actually want Base64

Base64 earns its keep in a handful of well-defined situations:

Data URIs: embedding files directly

The most common modern use. A data URI lets you inline a small file (an image, a font, an SVG icon) directly into your HTML or CSS instead of linking to a separate file:

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

The browser decodes the Base64 and renders the image, with no extra network request. For tiny, frequently used assets (a logo, an icon, a background pattern) this can make a page feel snappier by removing round-trips. The trade-off is that data URIs can't be cached separately and inflate your HTML/CSS, so they suit small assets only.

Email attachments

Email was built for text, so every binary attachment you've ever sent was Base64-encoded behind the scenes (via MIME) to travel safely and reassembled on the other end. You never see it, but it's why an image can ride inside a plain-text email protocol at all.

Embedding binary in JSON, XML, or config

JSON has no native binary type. If you need to tuck a small file, a certificate, or a binary blob inside a JSON payload or a YAML config, Base64-encoding it into a string is the standard workaround. It's also common in API responses that return image data inline.

Basic authentication and tokens

HTTP Basic Auth Base64-encodes the username:password pair into a header. And as our guide on the anatomy of a JSON Web Token explains, a JWT's header and payload are Base64URL-encoded, which is exactly why you can decode a token and read its claims.

When not to use Base64

Just as important as the good uses are the misuses:

  • It is not security. This is the big one. Base64 is trivially reversible. Anyone can decode it in seconds. Encoding a password or secret in Base64 hides it from no one. If you need confidentiality, you need encryption, which is a completely different thing.
  • Not for large files. The 33% size penalty means a big Base64-encoded file wastes bandwidth and memory. Serve large assets as normal binary files over HTTP. Save Base64 for small inline pieces.
  • Not compression. Base64 makes data bigger, never smaller. If you're trying to shrink something, you want a compression tool, not an encoder.

Encoding and decoding it yourself

Because Base64 is a reversible transform, you can move data both directions freely. Paste text or drop a file into our Base64 encoder to produce an encoded string, handy for building a data URI or embedding a small asset. Going the other way, our Base64 decoder turns an encoded string back into its original text or file, which is perfect for inspecting a data URI, reading a config value, or checking what's inside a token's payload. Both run entirely in your browser, so even sensitive data stays on your device.

If you're working with encoded data inside structured formats, it helps to be fluent in those too, our guide on what JSON is and how to read it covers the container that so often holds Base64 strings.

Encoding, not encryption

Base64 is a transport encoding, not encryption and not compression. It re-expresses binary data using 64 text-safe characters (three bytes in, four characters out, about 33% larger) so files can travel through channels built only for text. Reach for it to inline small assets as data URIs, to carry binary inside JSON or email, and to read token payloads. Never reach for it to hide secrets or shrink big files.

Need to encode or decode something right now? Try the Base64 encoder and Base64 decoder, free, instant, and processed entirely in your browser.

Encode something and read it back

The Base64 encoder runs the browser's own encoding primitives on your text or file, locally, and shows the round trip both ways. Two habits it makes easy: check the 33% size growth on a real file before you inline it anywhere, and decode any Base64 blob you find in a config or a token to see what it actually holds. Since decoding happens in your tab, pasting a suspicious string is safe in a way that pasting it into a random website is not.

Sources

Written by

Chandrabhan Shekhawat

Founder of Gigai Kripa Services. Builds the 250+ privacy-first browser tools on this site and writes the guides that go with them.

6 mins read

Never miss a guide

New tools and how-to articles land regularly. Follow along however you like. No inbox required.