Basics

What is base64 — and why it is not encryption

Illustration: a letter being repacked into neat tiles while a padlock lies unused nearby

Look at this string: eyJlbWFpbCI6ImFubmFAbWFpbC5ydSJ9. Looks like a cipher, right? Letters, digits, sometimes == at the end. Plenty of people see that and conclude the data is protected.

Now the trick. Open a terminal and type:

echo 'eyJlbWFpbCI6ImFubmFAbWFpbC5ydSJ9' | base64 -d

You get {"email":"[email protected]"}. No password, no key, half a second. That's base64: it doesn't hide your data, it changes its clothes. And confusing "re-dressed" with "hidden" causes a whole class of leaks.

Why re-dress data at all

The problem base64 solves sounds boring, but it's real: some channels only carry text.

An image, a PDF, a zip — those are bytes. Any bytes, including ones that match no letter at all. Now try stuffing such a byte into JSON, into an email body, into a link, or into an environment variable. JSON expects text. Email protocols were designed when "text" meant Latin letters. A link breaks on a space.

Base64 is a translator from "arbitrary bytes" into "safe characters": 26 lowercase, 26 uppercase, 10 digits and two extras. Sixty-four in total — hence the name. That set survives anywhere.

How it works: three bytes into four characters

The mechanics are simple. Base64 takes your data 3 bytes at a time (24 bits), slices it not into bytes but into four 6-bit chunks, and replaces each chunk with one character from its alphabet.

Two consequences worth knowing up front:

  • The data grows by about a third. Three characters in, four out. A 300 KB image inlined into HTML as base64 weighs around 400 KB. That's not compression, it's the opposite.
  • A trailing = is padding, not a signature. If the byte count doesn't divide by three, one or two = are appended so the length stays a multiple of four.

There is no key anywhere in this scheme. One public algorithm, identical for everyone — which is exactly why anyone can decode it without a password.

Why it is not encryption

The difference between encoding and encryption is whether a secret is involved:

  • Encoding (base64) — reversible rewriting by a public rule. No secret. The goal is to deliver data through a channel that would otherwise mangle it.
  • Encryption — turning data unreadable using a key. No key, no way back. The goal is to hide.
  • Hashing — a one-way squeeze that never unrolls. That's how passwords are stored.

The most common beginner trap: "I'll base64 my API key so it doesn't sit in the code in plain sight." It buys you nothing — the string gets copied and decoded. Keys live in environment variables and in your host's secret store, and there's a separate walkthrough on doing that properly.

The second classic case is a JWT token. Its middle section is base64url-encoded, and inside sits plain text: your id, email, expiry. Anyone holding the token can read all of it. The signature protects against forgery, not against reading. So never put anything in a JWT that the token's owner shouldn't see.

Where you'll run into it

Base64 isn't an abstraction — it's already lying around in your project:

  • data:image/png;base64,iVBOR… in CSS or HTML — an image inlined into the code. Handy for tiny icons, harmful for big files: such an image can't be cached separately by the browser and bloats the page.
  • Email attachments. Every PDF in an email travels the wire as base64.
  • Keys and certificates in environment variables. A service-account.json file often gets flattened with base64 -w0 file.json — otherwise the line breaks wreck the config.
  • API responses with binary data — images from generation models arrive exactly like this.

Which gives you a practical everyday rule: when you see base64, treat it as plain text in a costume. Want to know what's inside? Decode it. Want nobody to know? Encrypt it — encoding won't do the job.

How to encode and decode it yourself

Two ways you'll use today:

  • In the terminal. base64 file.png encodes, base64 -d brings it back. The -w0 flag matters more than it looks: without it the output is split into 76-character lines, and that "multi-line" key wrecks whatever config you paste it into.
  • In the browser console. btoa('hello') encodes, atob('aGVsbG8=') decodes.

And here's the trap everyone hits: btoa throws an error on non-Latin text and emoji. The reason is base64's nature — it works on bytes, and btoa expects characters that fit in a single byte. "Привет" doesn't fit. The right path is to turn the text into bytes first (new TextEncoder().encode(str)), then encode those.

Can base64 be decoded without a key?

Yes, always — base64 has no key. One terminal command (base64 -d) or any online decoder returns the original data.

How is base64url different from base64?

Regular base64 uses + and /, and both have special meaning inside links. base64url swaps them for - and _, and usually drops the trailing =. That's the variant used in JWTs and URL parameters.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →