Irreva logo
Explore Irreva
DeveloperApril 13, 2026· 6 min read· Updated June 10, 2026

What Is the Difference Between Base64 and URL Encoding

Hasanur Rahman

Written by Hasanur Rahman

Founder & Full-Stack Developer · Irreva · Rangpur, Bangladesh

Base64 and URL encoding are both ways to represent data as safe text characters, but they serve completely different purposes. Base64 encodes binary data into ASCII text. URL encoding makes arbitrary characters safe to use in a URL. Confusing them leads to double-encoding bugs, garbled data, and security issues. Here's how each works and when to use them.

What Base64 encoding does

Base64 encodes any binary data into a string of 64 printable ASCII characters: A–Z, a–z, 0–9, +, and /. It represents 6 bits of data per character, so every 3 bytes of input become 4 Base64 characters. Padding (=) is added to make the output length a multiple of 4.

The primary use case is embedding binary data in text-based formats. MIME email attachments were the original use: binary files (images, documents) were Base64-encoded to be embedded in the text-based email format. Today, Base64 is used for data URIs (embedding images in HTML/CSS), storing binary data in JSON, and representing cryptographic keys in PEM format.

Base64 is not encryption or compression. It's encoding — anyone can decode it trivially. The output is about 33% larger than the input. Do not use Base64 to 'protect' sensitive data.

URL encoding (percent encoding)

URL encoding replaces characters that are unsafe in a URL with a percent sign followed by the character's hexadecimal code. Spaces become %20, & becomes %26, = becomes %3D. The purpose is to allow arbitrary text to be safely included in URL components.

URL encoding is not about binary data — it's about making text characters safe for URL syntax. Its scope is much narrower than Base64: it only needs to handle the characters that have special meaning in URLs or that aren't valid ASCII.

The encoding is applied to specific URL components (query parameter values, path segments) and should not be applied to the entire URL structure.

Base64URL — the version used in JWTs

Standard Base64 uses + and / which are not safe in URLs. Base64URL is a variant that replaces + with - and / with _, making the encoded string safe to use directly in a URL without further encoding. Padding (=) is also omitted.

Base64URL is what JWTs use for their header and payload sections. It's also used in OAuth PKCE (Proof Key for Code Exchange) and other security protocols where base64-encoded data must appear in URLs.

When decoding a JWT, the correct function is base64url decode, not standard base64 decode. If you use the wrong variant, you'll get garbage output on any token containing + or / in the encoded data.

  • Base64: uses + and /, includes padding =
  • Base64URL: uses - and _, omits padding — safe for URLs
  • URL encoding: encodes individual characters as %XX — different purpose entirely

When to use each

Use Base64 when you need to embed binary data (images, files, keys) in a text format like JSON, HTML, or email. Use Base64URL when that encoded data also needs to be URL-safe (JWT sections, OAuth tokens, signed URLs).

Use URL encoding when you're constructing a URL and need to include special characters in query parameter values or path segments. In JavaScript, use encodeURIComponent() for individual parameter values and encodeURI() for complete URLs.

Don't mix them up. Double-encoding — URL-encoding a Base64 string that's already using safe characters — adds unnecessary %3D for the = padding and %2B for + characters, producing URLs that work but are unnecessarily verbose.

Frequently Asked Questions

Can I decode a Base64 string in my browser?

Yes. JavaScript provides btoa() to encode and atob() to decode Base64. For Base64URL, you need to replace - with + and _ with / before passing to atob(), and re-add any removed padding.

Why does Base64 output end with == sometimes?

Base64 encodes 3 bytes at a time. If the input length isn't a multiple of 3, the output is padded with = characters to reach a multiple of 4. One = means the last group had 2 bytes, two == means the last group had 1 byte.

Is Base64-encoded data secure?

No. Base64 is trivially reversible by anyone — it's encoding, not encryption. Never use Base64 to protect sensitive data. It only converts binary to text; it provides no security whatsoever.

What is a data URI?

A data URI is a URL scheme that embeds data directly in the URL. An image as a data URI looks like: data:image/png;base64,iVBORw0K... The image data is Base64-encoded. This embeds the image inline in HTML/CSS without a separate HTTP request.

Do I need to URL-encode the output of Base64 encoding?

Standard Base64 output may contain +, /, and = which need URL-encoding if placed in a URL. Use Base64URL encoding instead (replaces + with -, / with _, drops =), which produces URL-safe output that doesn't need further encoding.

Hasanur Rahman

About the author

Hasanur Rahman

Founder & Full-Stack Developer · Irreva · Rangpur, Bangladesh

Hasanur Rahman is the founder of Irreva and a full-stack developer based in Rangpur, Bangladesh. He builds all of Irreva's tools with a focus on privacy-first, browser-based processing.