Three encodings, one thing in common

Base64, URL encoding and HTML entities are three transformations developers meet every day. They solve different problems but share one property: all three are encodings, not encryption. There is no key, no confidentiality, and anyone can reverse them. Their purpose is not to hide data but to make it travel intact through a particular medium. Once that distinction is clear, choosing the right one becomes easy.

Base64: turning binary into text

Some channels carry only text: email bodies, JSON, XML, certain database fields. To push binary data such as an image or a PDF through them, you convert every 3 bytes into 4 characters drawn from a safe 64-character alphabet. The output grows by roughly a third; that is the price of Base64.

Where it is used:

  • Email attachments (MIME) and small images embedded in HTML or CSS (data:image/png;base64,...).
  • Carrying a binary field inside JSON.
  • The HTTP Basic Auth header, which carries username and password; this provides no confidentiality and is equivalent to plain text without HTTPS.
  • The parts of a JWT (in the Base64URL variant: - and _ instead of + and /, and no padding).

Where it is not used: "we stored the password in Base64" means the password is stored in the clear. Embedding large files in a database as Base64 inflates both storage and query time; store the file on disk and the path in the database.

URL encoding: characters a URL cannot carry

Only certain characters may appear freely in a URL. Spaces, accented letters, &, ?, = and # either break the structure or do not survive transport. URL encoding (percent-encoding) replaces them with % followed by two hex digits: a space becomes %20, é becomes %C3%A9, & becomes %26.

The critical question is what to encode. Encode only parameter values, never the whole URL. Otherwise ? and = get encoded too and the server cannot see the parameters at all. This is the most common problem for marketers building UTM parameters: if utm_campaign=summer sale & coupon is appended without encoding, the & splits the parameter in two and the campaign name shows up truncated in analytics. The correct form is utm_campaign=summer%20sale%20%26%20coupon.

Be aware that in form submissions a space may also appear as +. Server-side libraries decode both, but if you parse query strings by hand, handle both.

HTML entities: telling the browser "this is text, not a tag"

In HTML, < starts a tag. If you print a user's comment onto a page unchanged and it contains <script>, the browser executes it; that is exactly what a cross-site scripting (XSS) vulnerability is. HTML entity encoding turns five characters into safe equivalents: &lt;, &gt;, &amp;, &quot; and &#39;. The browser displays them as text instead of interpreting them as markup.

The rule: every string that comes from a user or an external source is encoded when written into HTML. Most template engines do this by default; trouble starts when "raw output" escape hatches are used without thinking. HTML entities apply only to the HTML context. If you place the same string inside a JavaScript string or a URL, that context needs its own encoding.

Which one where, at a glance

  1. Move binary data through a text channel → Base64
  2. Put a value into a URL parameter → URL encoding (the value only)
  3. Display a string on an HTML page → HTML entities
  4. Hide data → none of them; you need real encryption
  5. Detect whether data changed → none of them; you need a hash

Common mistakes

  • Double encoding: encoding a value that is already encoded. %20 becomes %2520, and the server sees the literal text %20 instead of a space. Encode once, at the moment the data is written into its target medium.
  • Not encoding at all: putting a file name with accented characters straight into a URL. Some clients fix it silently, others return 404.
  • Wrong context: embedding HTML-entity-encoded text in JavaScript, or printing URL-encoded text into HTML. Each context has its own encoding.
  • Mistaking Base64 for secrecy: the "encrypted" API key in a configuration file is very often just Base64.

Conclusion

Encoding transports data, encryption hides it, hashing verifies it. Keeping the three apart protects you from vulnerabilities like XSS and from hours of debugging questions such as "why is my campaign name cut in half". To quickly see the Base64, URL-encoded and HTML-entity forms of a string, or to decode one, use the encoding tool.