Base64 is encoding, not encryption
Base64 converts binary data into 64 safe text characters (A–Z, a–z, 0–9, +, /) so it can travel through systems that only accept text. Anyone can decode it instantly. Never treat Base64 as protection for passwords, tokens or personal data.
Why the equals signs
Base64 works on groups of three bytes, which map to four characters. When the input length is not a multiple of three, the output is padded with = so the decoder knows the last group is short. One = means one byte remained, two means two.
As a consequence, Base64 output is about 33% larger than the input. That overhead is the price of text-safety.
Where Base64 is actually used
- Data URIs — embedding small images or fonts directly in CSS/HTML
- Email attachments — MIME encodes binary files as text
- JSON Web Tokens — the three parts of a JWT are Base64URL-encoded (using
-and_instead of+and/) - API payloads — sending binary blobs inside JSON
Percent-encoding (URL encoding)
URLs may only contain a limited set of characters. Anything else must be percent-encoded: the character’s byte value in hex, prefixed with %. A space becomes %20, and a non-ASCII character such as 中 becomes a multi-byte sequence like %E4%B8%AD.
encodeURI vs encodeURIComponent
encodeURI— encodes a complete URL but leaves/,?,&,=intact. Use it on a whole URL you are about to display.encodeURIComponent— encodes a value that will be placed inside a URL parameter, so/and&are escaped too. This is the one you usually want for query values.
Using encodeURI on a query value is a classic bug: an & inside the value silently splits it into two parameters.
HTML entities
A separate layer: characters such as < and & must be escaped as < and & when inserted into HTML, or the browser will treat them as markup. HTML escaping protects against injection; percent-encoding protects URL syntax. They are not interchangeable.
How Base64 works, step by step
Take the three bytes of the word “Man”: 77, 97 and 110. Written in binary, those 24 bits split into four six-bit groups, and each group is looked up in the Base64 alphabet. The result is TWFu. Every output character carries six bits rather than eight, so four characters hold the same information as three bytes — that is the 4:3 ratio behind the 33% size increase.
input M a n
bytes 01001101 01100001 01101110
sextets 010011 010110 000101 101110
base64 T W F n
Decoding reverses the lookup. The padding rule is simple arithmetic: one leftover byte produces two characters and one =; two leftover bytes produce three characters and two equals signs. Padding in the middle of a string is invalid, and a decoder should reject it rather than guess.
Standard Base64 versus Base64URL
The standard alphabet uses + and /, which are legal in a URL path but not always safe inside a query value without escaping. Base64URL replaces them with - and _ and usually drops the padding entirely. The bytes are identical; only the alphabet differs. Mixing the two is a common failure: a token encoded with the URL variant cannot be decoded by a standard decoder until those four characters are translated. This is exactly why JSON Web Tokens use the URL-safe form.
Misconceptions worth clearing up
- “Base64 hides the data.” It does not. Anyone can decode it in one command, and decoded output is often recognisable. Use encryption, not encoding, when confidentiality matters.
- “Base64 is a checksum.” It detects nothing. A corrupted character decodes to different bytes with no error, so integrity needs a hash or a signature.
- “Decoding twice is harmless.” Double encoding is a real bug when one layer expects raw text and receives an already-encoded value. Decode once, at the boundary where the data enters your system.
- “Long lines are fine.” MIME wrappers insert line breaks every 76 characters for email, and some decoders reject or silently ignore them. Know which variant you are feeding to which decoder.
- “A data URI is free.” Inlining a 200 KB image as Base64 makes the parent document 33% larger and delays first paint, because the browser must download and parse it before rendering. Inline only small icons.
Percent-encoding details that trip people up
- Reserved characters are context-dependent.
/,?,&and#carry structural meaning in a URL; a value containing them must be encoded so they cannot be mistaken for syntax. - Form encoding is not the same as URL encoding.
application/x-www-form-urlencodedtraditionally turns spaces into+, while a path segment uses%20. A+sent to a server that does not expect it stays a plus sign. - Encoding happens on bytes. Percent-encoding works on bytes, so a non-ASCII character is converted to UTF-8 and then escaped byte by byte. Encoding the code point directly can produce a string that different servers interpret differently.
- encodeURIComponent is not complete. It leaves
! * ' ( )unescaped because they are permitted in a query component. If a downstream system is stricter, escape them yourself. - Do not encode the whole URL twice. Building a redirect from an already-encoded
nextparameter is a classic source of%252Fin logs, since%25is the encoded percent sign.
Which encoding for which layer
| Mechanism | Purpose | Typical failure if skipped |
|---|---|---|
| Base64 | Carry binary through text channels | Unreadable bytes, broken transport |
| Percent-encoding | Keep URL syntax unambiguous | Parameters split, paths truncated |
| HTML escaping | Keep markup separate from text | Injection, broken layout |
| Encryption | Protect content from readers | Data exposed in plain sight |
Practical checklist
- Use Base64 only to move bytes through text, never to protect them.
- Choose Base64URL for URLs, cookies and JWT segments; translate characters rather than re-encoding from scratch.
- Decode once, close to where the value is used, and validate the result before trusting it.
- Use
encodeURIComponentfor query values andencodeURIonly for a complete URL you intend to display or request. - Keep inlined data URIs small and remember that they are cached with the parent document.
- Compare the decoded bytes, not the encoded strings, when debugging a mismatch between two systems.
Encode and decode both formats in the browser with our Encode / Decode tool. Related reading: URL anatomy and query parameters, embedding images as data URIs and JWT structure and expiry.