⚡ SmartDevTools

Base64 Is Encoding, Not Encryption

By SmarTech Apps · Published 2026-11-18 · 6 min read

Base64 shows up in HTTP headers, config files, tokens and e-mail, and it looks scrambled enough that people regularly mistake it for protection. It is not protection. Base64 is a way of writing arbitrary bytes using a small, safe alphabet, and anyone can reverse it in a second without any key.

What Base64 is for

Many older text-based protocols were designed around 7-bit ASCII. E-mail bodies, HTTP headers, JSON strings and XML documents all choke on arbitrary binary data: a stray zero byte, a newline or a byte above 127 can truncate, corrupt or re-encode the payload. Base64 solves that by mapping every byte sequence onto 64 characters that survive nearly any text channel: A-Z, a-z, 0-9, + and /, plus = as padding.

That is the whole job. There is no key, no secret and no algorithm parameter. Given the same input everybody produces the same output, and given the output everybody can recover the input. That property is exactly what makes it useful for transport and exactly what makes it useless for secrecy.

How the encoding works

Base64 reads the input three bytes (24 bits) at a time and re-slices those 24 bits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, which indexes into the alphabet: 0 to 25 are A-Z, 26 to 51 are a-z, 52 to 61 are 0-9, 62 is + and 63 is /.

Take the three ASCII characters Man. Their byte values are 77, 97 and 110:

M = 77  = 01001101
a = 97  = 01100001
n = 110 = 01101110

concatenated: 010011010110000101101110
regrouped:    010011 010110 000101 101110
decimal:         19     22      5     46
alphabet:         T      W      F      u

So Man becomes TWFu. You can check this in any language; in Python, base64.b64encode(b"Man") returns b'TWFu'.

Padding

Input lengths that are not a multiple of three leave a partial group at the end. One leftover byte yields two Base64 characters followed by ==; two leftover bytes yield three characters followed by a single =. The four-byte string Hi!? is a full group plus one leftover byte, so it encodes to SGkhPw==. Padding carries no information of its own. It only keeps the output length a multiple of four, and many modern formats (JWT among them) simply strip it.

The size cost

Three bytes become four characters, so the output is about 33% larger than the input (more precisely, 4 × ceil(n/3) characters). A 300 KB image embedded as Base64 costs roughly 400 KB, and because it is then part of a text document it also compresses worse than the raw file would on its own. Keep that in mind before inlining anything large.

Base64url and where it is used

The standard alphabet has two awkward characters. + means a space in form-encoded query strings and / is a path separator, and = has meaning in query strings too. The URL-safe variant, usually called base64url, swaps + for - and / for _, and commonly drops the padding. It is the variant used for JSON Web Tokens, which is why you can paste a token segment into a URL without escaping. The mechanics of that are covered in How JWT Works, and you can inspect real tokens with the JWT Decoder. If you are choosing between Base64 and percent-encoding for a value in a URL, see URL Encoding Rules.

To convert by hand, replace - with + and _ with /, then add = until the length is a multiple of four. Be aware that our Base64 Encoder / Decoder works with the standard alphabet only: it has Encode and Decode modes and no URL-safe switch or data-URI option, so do that substitution yourself for base64url input.

Data URIs

A data URI embeds a file directly in a document: data:image/png;base64,iVBORw0KGgo.... The browser decodes the Base64 and treats the result as if it had been downloaded. It saves an HTTP request for tiny icons and is common in CSS and in HTML e-mail, but the 33% overhead, the lack of separate caching and the bigger HTML make it a poor choice for anything beyond a few kilobytes.

Text, bytes and the UTF-8 trap

Base64 encodes bytes, not characters. To encode text you first need to turn it into bytes, and the choice of character encoding decides the result. The same string gives different Base64 under UTF-8 and UTF-16, so both sides must agree. UTF-8 is the sensible default.

In the browser this bites people through btoa(). It expects a string in which each character is a single byte (code points 0 to 255). Pass it anything outside Latin-1, such as "✓" or most non-English text, and it throws an InvalidCharacterError. The fix is to encode to UTF-8 bytes first, which is what TextEncoder does. Our encoder takes this route: it converts your input to UTF-8 before encoding and decodes as strict UTF-8, so the text héllo ✓ becomes aMOpbGxvIOKckw==. A side effect is that decoding Base64 that holds non-UTF-8 binary (a PNG, say) reports an error instead of printing garbage, because the bytes are not valid text.

// browser
const bytes = new TextEncoder().encode("héllo ✓");
const b64 = btoa(String.fromCharCode(...bytes));   // aMOpbGxvIOKckw==

# python
import base64
base64.b64encode("héllo ✓".encode("utf-8"))   # b'aMOpbGxvIOKckw=='

Why Base64 is not a way to hide secrets

The word "encoded" gets confused with "encrypted" because the output looks opaque to a human glancing at it. A quick test: if you can reverse a transformation with no secret, it provides no confidentiality. Base64 fails that test completely. Two common places where this causes real trouble:

The same goes for API keys "obfuscated" in a mobile app, tokens in a cookie, or passwords stored in a database column as Base64. An attacker needs one line of code. A JWT payload is likewise only base64url JSON: it is signed so it cannot be altered, not hidden, so never put anything confidential in it.

What to use instead

Choose the tool for the property you need. If you need secrecy, use real encryption such as AES-GCM with a properly managed key, and Base64-encode the ciphertext only if you must put it in a text field. If you only need to detect tampering or compare values, use a hash or HMAC; the differences are laid out in Hashing vs Encryption, and you can compute SHA digests with the Hash Generator. For passwords you do not want to store reversibly at all, use a slow salted password hash. If you just need a strong random secret to start with, the Password Generator creates one.

Base64 remains the right answer when the question is "how do I carry these bytes through a text-only channel?". Treat it as plumbing, never as a lock.

Try the tools

Keep reading