Skip to content
All posts
8 min read(updated October 20, 2026)

URL-safe Base64 explained: alphabets, padding, and JWTs

Standard Base64 breaks URLs and filenames. What the RFC 4648 §5 alphabet changes, why padding is often stripped, how JWTs decode segment by segment, and how to convert between alphabets in one line.

  • guide
  • web

If you’ve ever pasted a Base64 string into a URL and watched everything after the + disappear, you’ve met the problem the URL-safe alphabet solves. This post covers the two RFC 4648 alphabets, what happens to padding, a segment-by-segment JWT decode, and the conversions that bite when you move a string between environments.

The two characters that cause all the trouble

Standard Base64 (RFC 4648 §4) uses this alphabet:

A–Z  a–z  0–9  +  /

The last two characters are the problem, and it pays to see exactly how a value decays in transit. Suppose an ID contains a+b/c==. Arriving inside a query string or an application/x-www-form-urlencoded form body, it hits the server’s URL-decoding step first — most frameworks decode query strings using the form-urlencoded rules, which predate RFC 3986’s stricter grammar — and there + is the wire encoding of a space, so your code receives a b/c== — one character silently corrupted, and the first two 6-bit groups now decode to entirely different bytes. Same value in a URL path: / splits it into two path segments, so a+b/c becomes directories named a+b and c, and the router sees a hierarchy nobody created. Nothing threw an error; the string was merely interpreted as structure it was never meant to be.

RFC 4648 §5 defines a variant alphabet for exactly these contexts — base64url:

A–Z  a–z  0–9  -  _

Only two characters change: + → - and / → _. Both replacements are unreserved URL characters — they survive every stage of URL parsing untouched — and every major filesystem accepts them. Everything else, the 6-bit grouping and the 33% size overhead, is identical. Here are the two alphabets side by side:

RFC 4648 §4 (standard) RFC 4648 §5 (URL-safe)
Index 62 + -
Index 63 / _
Padding =, required in strict decoders usually omitted
Bytes per 4 chars 3 3
Legal in URL query unencoded no (+ → space) yes
Legal in a filename no (/ on all major OSes) yes

Converting between the two alphabets is a plain character substitution, which is why every serious Base64 tool handles both transparently.

Filename legality, character by character

The URL problem generalizes to any context where punctuation carries meaning. Filenames are the sharpest example, because the rules differ per platform:

Character Windows macOS (APFS) Linux (ext4) Notes
/ illegal illegal illegal path separator everywhere
\ illegal legal legal Windows path separator; legal elsewhere
+ legal legal legal trouble only for URL contexts
= legal legal legal trouble when appended to a command line or ini-style key
: illegal (except drive letters) legal legal also NTFS alternate data streams
-, _ legal legal legal the two base64url additions

So a standard-alphabet Base64 string is never a safe filename on any operating system — with random underlying bytes it contains / about once every 64 characters, and which of the other punctuation lands in it is luck. Base64url strings are safe everywhere; that single property is why S3 object keys, CDN filenames, and browser-generated download names (non-ASCII filenames ride in the Content-Disposition header’s ext-value syntax, defined by RFC 5987 and applied by RFC 6266, which mangles them unless encoded) all use it.

Padding: keep it, or strip it?

Standard Base64 pads the final group with = characters so every encoded output has a length that’s a multiple of four. That’s useful when you’re concatenating or wrapping Base64 in larger protocols. But in a URL segment or a filename, = is yet another character with special meaning — so in URL-safe contexts the padding is usually omitted.

This is safe only because the decoder knows how much data it’s getting some other way — the length of the string itself, or the boundaries of the field carrying it. A decoder can reconstruct the padding because the remainder of length mod 4 fully determines it:

  • remainder 0 → nothing was stripped
  • remainder 2 → append ==
  • remainder 3 → append =
  • remainder 1 → the input was never valid Base64

That’s exactly what a robust decoder does behind the scenes: accept missing padding, note that it did so, and decode anyway. (Try it: paste a padding-less string into the Base64 decoder — it just works, and the status bar tells you.)

Some runtimes refuse to make that inference for you, and the difference is a famously annoying one. Python’s base64.urlsafe_b64decode throws binascii.Error: Incorrect padding on stripped input — the community workaround is so universal that the line s + '=' * (-len(s) % 4) appears in half the JWT tutorials on the internet. .NET’s Convert.FromBase64String throws FormatException unless the padding is re-added first. Go splits the difference within one package: base64.URLEncoding expects padding and rejects a stripped string, base64.RawURLEncoding accepts stripped and rejects padded — pick the wrong one and a perfectly correct string dies with “illegal base64 data”. Browser-native atob fails differently: it throws InvalidCharacterError on _ and -, because they were never in its alphabet at all. The rule to carry with you: feed a stripped string only to a Raw decoder, and a padded one only to a padded decoder — or re-add the padding yourself before handing it to a strict one.

How JWTs use it

JSON Web Tokens (RFC 7519) are the most common place you’ll see base64url in the wild. A JWT has three dot-separated parts:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Each part is base64url-encoded without padding. The dots are the only separators, so a parser can split the token without worrying about = characters, and no part can contain a / that would break routing. Notice the string already shows the alphabet at work: _adQ in the signature contains an underscore that a standard-alphabet encoder would have rendered as /.

Decode the header segment by hand to see the mechanics. eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 has length 36 — a multiple of four — so no padding was stripped. Map each of the 36 characters to its 6-bit index, concatenate the 216 bits, and regroup them into 27 bytes: the JSON {"alg":"HS256","typ":"JWT"} falls out, 27 UTF-8 bytes matching perfectly. The payload eyJzdWIiOiIxMjM0NTY3ODkwIn0 is length 27 — remainder 3 — so a strict decoder must mentally append = before decoding; it yields {"sub":"1234567890"}. The third part is a signature: raw HMAC-SHA256 output in this token (alg is HS256), 32 random bytes, base64url’d into exactly 43 characters — remainder 3 again, and again stripped — meaningless as text, verifiable only with the key.

Two failure modes follow from all of this. A standard Base64 decoder that rejects missing padding throws on the payload above; a +//-only decoder throws even earlier, because it doesn’t recognize - and _ as alphabet characters at all. A decoder that accepts either alphabet automatically, like the tool on this site or the dedicated JWT inspector, saves you the manual search-and-replace.

Not the same thing: base64url vs base62 vs base58

The name similarity trips people up constantly, so it’s worth drawing the line. base64url is still Base64: the bit stream is identical to standard Base64, and converting between them is character substitution you can do with two replace calls. Base62 and Base58 are different encodings entirely — they treat the input as one giant integer and divide it by the radix repeatedly. This has several consequences:

  • Zero bytes matter. Leading zero bytes (or long runs of 0x00) contribute nothing to a giant integer, and arithmetic encoders skip them, so Base58 outputs of related inputs can differ in length. Base64 never does — its output length is a deterministic function of byte count.
  • No padding, no alignment. Base58 has no = and no equivalent of the mod-4 rule; you can’t infer validity from length.
  • Not bit-sliceable. You can’t decode Base58 without the whole string; arbitrary concatenation of chunks is impossible. Base64 can be split at any multiple of four characters.

That’s why Bitcoin addresses use Base58 (dropping 0OIl for human transcription), short-URL services use Base62, and standards bodies kept reaching for base64url (RFC 4648 §5) everywhere a URL or filename is involved. The what-is-Base64 post covers the mechanics.

Cheat sheet: converting between alphabets

Each snippet assumes the input string s:

Python

import base64
b = base64.urlsafe_b64encode(s if isinstance(s, bytes) else s.encode())   # standard → base64url, padded
u = b.rstrip(b'=').decode()          # strip padding for JWT-style output
m = base64.urlsafe_b64decode(u + '=' * (-len(u) % 4))   # back to bytes, re-adding padding

JavaScript (and TypeScript)

const url = s.replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '');
const std = url.replaceAll('-', '+').replaceAll('_', '/') + '='.repeat((4 - url.length % 4) % 4);

The JS version is worth internalizing: padding removal is replace(/=+$/, '') (it can only ever be at the end), and re-adding is repeat((4 - len % 4) % 4) — the (4 − len mod 4) mod 4 shape handles the already-padded case in one expression.

Which one should you use?

  • URLs, filenames, JWTs, query parameters → base64url, padding omitted.
  • Email (MIME), PEM certificates, embedded data in XML/JSON → standard Base64, padding kept, and wrap lines at 76 (MIME) or 64 (PEM) characters.

Both tools on this site expose this as a first-class option rather than an afterthought: the encoder has a URL-safe toggle plus an “omit padding” switch, the decoder accepts either alphabet automatically — no mode to pick — and if you already have data in one alphabet and need it in the other, the format converter performs exactly this substitution as its main job.

One final warning that belongs in every Base64 article: URL-safe Base64 is still not encryption. A JWT’s payload is readable by anyone who obtains the token — decode it and see. Signing (the third part) guarantees integrity, not secrecy.

Try it yourself — right now

Both tools run 100% in your browser. Nothing you type or drop is ever uploaded.

Related posts

esc

Tools

Pages

↑↓ navigate↵ open⌘K

Keyboard shortcuts

Same shortcuts everywhere — ⌘ on macOS, Ctrl on Windows and Linux.