URL encoding and Base64 both turn readable text into unreadable text, and neither one hides anything. There is no key, no password and no secret: any decoder — including one built into every browser, every programming language and every spreadsheet add-on — reverses them in milliseconds. They solve transport problems, not privacy problems. Percent-encoding makes arbitrary bytes safe to place inside a URL; Base64 makes arbitrary bytes safe to place inside text-based channels such as JSON, XML, email bodies and data URLs.
The confusion is understandable. A Base64 string has no words in it, so it looks encrypted. A query string full of %E4%BD%A0%E5%A5%BD looks like ciphertext. Both are the textual equivalent of repackaging: the content is intact, only the wrapping changed.
What percent-encoding actually does
A URL has a reserved set of characters with structural meaning: ? starts the query, & separates parameters, # starts the fragment, / separates path segments, = assigns a value. If your data contains one of those characters, the parser will treat it as structure and the URL will mean something you did not intend. Percent-encoding replaces the offending character with a percent sign followed by the two hexadecimal digits of its byte value.
| Character | Encoded | Why |
|---|---|---|
| space | %20 | Not allowed literally in a URL |
& | %26 | Would start a new query parameter |
/ | %2F | Would start a new path segment |
? | %3F | Would start the query string |
# | %23 | Would start the fragment |
| 你 | %E4%BD%A0 | UTF-8 bytes, three per CJK character |
Non-ASCII characters are first encoded as UTF-8 bytes and then percent-encoded byte by byte. That is why one Chinese character becomes three escape sequences, and why a Chinese sentence in a correctly encoded URL looks three times longer than the text it represents. It also explains a classic bug: an encoder that uses the system's legacy code page instead of UTF-8 produces escapes that decode to mojibake in every other system.
Where URL encoding goes wrong
- Double encoding. Encoding an already-encoded string turns
%20into%2520, which decodes back to%20rather than a space. Every retry through a redirect or a proxy adds another layer. - Encoding the whole URL. If
https://example.com/text/a bis encoded as one string, the colon and slashes become%3Aand%2Fand the result is no longer a URL — it is a search term. Encode the components, not the assembled URL. - Space versus plus. In
application/x-www-form-urlencodeddata — the body of a normal HTML form — a space may be written as+. In a path segment,+is a literal plus. Decoders differ; a value that came from a form body may need+converted to a space before decoding, and a value from a path must not be. - Encoding too little. Leaving
&or#unescaped inside a parameter value silently truncates the value at the parser. - Encoding key names and separators. Encoding the
=between a key and its value turns one parameter into an unparseable blob.
What Base64 actually does
Base64 takes every three bytes of input — 24 bits — and rewrites them as four characters from a 64-character alphabet: A–Z, a–z, 0–9, plus + and /, with = as padding when the input length is not a multiple of three. The size grows by about 33%, which is the price of turning arbitrary binary into text that survives channels designed for text.
It exists because email, JSON, XML and HTTP headers historically could not carry raw binary reliably: control characters get mangled, line lengths get wrapped, encodings get converted. Base64 sidesteps all of that by using only characters no transport will touch. That is the entire purpose. Nothing is hidden, and the original is recoverable with certainty — which is precisely what makes it unsuitable for secrets.
The URL-safe variant replaces + with - and / with _, so a Base64 value can sit inside a URL or a filename without percent-encoding. JWTs use exactly this variant, then strip the padding.
The Unicode bug hiding in every browser
The browser functions btoa() and atob() operate on "binary strings" — one character per byte, with character codes limited to 0–255. Pass them a string containing 你 or an emoji and they throw InvalidCharacterError, because the code point is above 255. This is the single most common Base64 bug in JavaScript, and it appears in a thousand code snippets that work perfectly in English and fail the moment a customer types an accent.
The fix is to encode to UTF-8 bytes first — new TextEncoder().encode(text) — assemble those bytes into a binary string, and pass that to btoa. Decoding runs in reverse: atob gives bytes, and TextDecoder turns them back into text. The TextBench encoder does this, which is why 你好世界 round-trips to 5L2g5aW95LiW55WM and back without loss.
One more trap on the decode side: if the decoded bytes are not valid UTF-8, a lenient decoder substitutes U+FFFD (the replacement character) instead of failing. The output then looks like text with a few diamonds in it, and the original bytes are gone.
Where Base64 gets misused
- "Hidden" API keys in front-end code. A key encoded in JavaScript is still delivered to the browser, visible in the network tab, and decodable by anyone who cares. If the browser can use it, the visitor can read it. Keys belong on a server, behind a proxy that adds them to outbound requests.
- JWT payloads treated as secret. The middle segment of a JSON Web Token is Base64url-encoded JSON. It is signed, not encrypted: anyone can read the claims, including user IDs and roles. Confidential claims need JWE or server-side storage.
- Passwords in query strings. A Base64 password in a URL is readable in browser history, proxy logs and referrer headers. It is not merely decodable; it is logged.
- Data URLs as a security boundary.
data:text/html;base64,…is a transport convenience. It executes if navigated to, which is why some browsers restrict top-level data URL navigation.
A simple test separates encoding from encryption: if you can decode it without supplying anything you do not already have in the file, it is encoding. Encryption requires a key that is not present in the ciphertext, and even then the guarantees depend entirely on the algorithm and the key management around it.
What to use when you need secrecy
| Goal | Correct tool | Not this |
|---|---|---|
| Confidentiality in transit | TLS (HTTPS) | Base64 in a query string |
| Confidentiality at rest | AES-GCM with managed keys | Base64, ROT13, "hidden" fields |
| Verify a payload was not altered | HMAC or a digital signature | Base64 (it is trivially editable) |
| Store a password | Argon2id, bcrypt or scrypt with a salt | Any reversible encoding or plain hash |
| Keep a key out of the client | A server-side proxy | Obfuscation or encoding in JS |
Hashing deserves its own note: it is one-way, so it is not encoding either. It is the right tool for integrity and password storage, and the wrong tool for anything you need to read back.
FAQ
Is Base64 the same as encryption?
No. Base64 is a reversible encoding with a fixed public alphabet and no key. Anyone can decode it, including automated scanners that decode every Base64-looking string they find. Encryption transforms data with a secret key so that decoding without the key is computationally infeasible.
Why does my Base64 string end with equals signs?
Padding. Base64 encodes three bytes at a time as four characters, so an input whose length is not a multiple of three leaves a partial group: one leftover byte produces two characters plus ==, two leftover bytes produce three characters plus =. The padding is not data; it tells the decoder how many bytes were in the final group. Some formats, including JWT, strip it and let the decoder re-add it.
Can I put a Base64 image directly in HTML?
Yes, as a data URL: <img src="data:image/png;base64,…">. It avoids one HTTP request and is convenient for small icons embedded in a single file. For anything large it is counterproductive — the payload grows by a third, it cannot be cached separately, and it blocks parsing of the document that contains it.
Encode and decode in the TextBench encoder, and read how slugs are built for the part of URL design that percent-encoding does not cover.
Written by the TextBench team. Last updated November 19, 2026.