Comparison
Base64, URL encoding and HTML entities
Three encodings that look similar and solve different problems. None of them is encryption — all three are completely reversible by anyone, and treating any of them as a way to hide a value is the mistake they are most often involved in.
Side by side
| Base64 | URL encoding | HTML entities | |
|---|---|---|---|
| Exists to | Carry binary through text | Make text safe in a URL | Show markup as text |
| A space becomes | Part of the encoded run | %20 or + | if non-breaking |
| Size change | +33% | Slightly larger | Slightly larger |
| Used in | Data URIs, email, JSON | Query strings, paths | HTML bodies and attributes |
| Any security value | None | None | Prevents XSS when output-encoded |
The short answer
Base64 for embedding a small file in text. URL encoding for anything going into a query string. HTML entities when user input is rendered into a page — that one genuinely prevents a cross-site scripting bug, which is the one case where an encoding is doing security work.
In more detail
None of these is encryption
Base64, URL encoding and HTML entities are all completely reversible by anyone, with no key and no secret. A value encoded in any of them is in plain sight. The reason this is worth stating is that Base64 looks sufficiently unreadable that it gets used to “hide” tokens and passwords in configuration files and client-side code, where it hides them from nobody.
Why Base64 makes a file bigger
It represents three bytes using four printable characters, which is where the roughly thirty-three per cent growth comes from. That is the price of passing binary through a channel that only tolerates text. It is a reasonable trade for a small icon inlined into a stylesheet and a bad one for anything large, where a separate cacheable file wins easily.
The one that does real security work
Encoding user input as HTML entities before rendering it into a page is what stops a submitted string being parsed as markup, which is the cross-site scripting bug in its simplest form. It has to happen at output, in the context the value lands in, and not once at input. That distinction is the difference between a fix and a false sense of one.