DEV Community

Cover image for Base64 Is Not Encryption: What Encoding Actually Does to Your Data
ToolPeko
ToolPeko

Posted on

Base64 Is Not Encryption: What Encoding Actually Does to Your Data

A Base64 string looks like a secret. It is a dense block of letters, digits, plus signs and slashes, often ending in one or two equals signs. Dense and unreadable feels protected, and that feeling is the whole problem.

Base64 hides nothing. It is a spelling system for bytes, a way to write binary data with a small set of safe characters. Anyone can read it back, instantly, with tools built into every operating system.

What the encoding actually does

Base64 works with an alphabet of 64 characters: A to Z, a to z, 0 to 9, plus, and slash. It reads the input three bytes at a time. Three bytes hold 24 bits, and 24 bits split neatly into four groups of six bits each.

Each six-bit group picks one character from the alphabet. Three bytes go in, four characters come out. The word Man is three bytes, and it encodes to TWFu.

echo -n "Man" | base64
# TWFu

echo "TWFu" | base64 -d
# Man
Enter fullscreen mode Exit fullscreen mode

Real input rarely divides evenly by three. When one byte is left over at the end, the output gets two characters and two equals signs. When two bytes are left over, the output gets three characters and one equals sign.

That padding is the = you see at the end of so many encoded strings. It is bookkeeping, not security. It tells the decoder how many bytes the last group really holds.

Why Base64 exists

Many channels were built to carry text and only text. Early email is the classic case, and the same limit shows up in JSON, config files, HTML, and URLs. Raw binary bytes can break these channels, because some byte values act as control characters or delimiters.

Base64 rewrites any byte stream into characters that survive the trip untouched. That is its whole job. It is a transport format, closer to putting a fragile object in a padded box than to locking it in a safe.

Where you meet it every day

Data URIs put small images and fonts directly inside CSS or HTML, as a long Base64 string after a short prefix. The browser decodes the string and renders the bytes.

JSON Web Tokens carry a header and a payload as Base64URL segments, a close cousin that swaps plus and slash for minus and underscore. Only the third segment, the signature, involves any cryptography. The famous header value eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 decodes to plain JSON: {"alg":"HS256","typ":"JWT"}.

Email attachments travel as Base64 inside MIME messages, wrapped into lines of 76 characters. Open the raw source of a message with an attachment and you will see page after page of it.

Basic authentication builds its header by encoding username and password together, joined by a colon. That header is one decode away from the raw credentials. Basic auth only stays safe because HTTPS encrypts the connection, never because the encoding hides anything.

The size cost

Four characters carry three bytes, so encoded data grows by about 33 percent. A 3 MB file becomes roughly 4 MB in encoded form. Email wrapping adds a little more on top, in the form of line breaks.

That growth is a fair price for safe transport. It is a poor deal for storage, and it is worth remembering when an API accepts encoded uploads. You pay the extra third on every request.

Encoding is not encryption

Encryption scrambles data with a key, using math designed to resist attack. Decoding it without the key should take longer than anyone is willing to spend. Base64 has no key, no secret step, and no resistance at all.

The alphabet is public. The steps are published in an open standard. Every laptop and phone on earth can reverse the process in milliseconds, and most have a base64 command already installed.

So a Base64 string protects its contents from nobody. Think of a session token sitting in a log file, or a password sitting in a config file. Encoding changes how the value looks, not who can read it.

For storage, logging, sharing, and redaction, treat Base64 text as the plain text it really is.

Decode locally, not on a stranger's server

Sooner or later you meet a string you did not write: an API response, a token segment, a log line. You want to know what it says before you act on it.

The quick route is pasting it into the first online decoder a search turns up. Most of those decoders run on a server. Your string might be a live token, a password reset payload, or a private document. Pasting it sends a copy to a machine you know nothing about.

Local options are not slower. A terminal decodes with base64 -d, and browser-based decoders do the same work on your own device. The string never leaves the machine, and the answer is just as fast.

Full disclosure: I build tools at ToolPeko, a free set of 300+ utilities that run in the browser. Its Base64 decoder decodes on your device, with no upload and no sign-up. It also accepts the messy forms real strings arrive in, such as data URI prefixes, line breaks, and the URL-safe alphabet.

Base64 is a good tool for the job it was designed for. It moves bytes safely through text-only channels, and it does that job well. Just never ask it to keep a secret, because keeping secrets was never part of the design.

Top comments (0)