The short answer
Base64 and Base64URL encode the same bytes the same way and differ in exactly two of their 64 characters, plus a convention about padding. Standard Base64 uses + and /, which both mean something else inside a URL. Base64URL replaces them with - and _ and usually drops the trailing =.
| Standard Base64 | Base64URL | |
|---|---|---|
| Defined in | RFC 4648, section 4 | RFC 4648, section 5 |
| Characters 62 and 63 | + and / | - and _ |
Padding with = | Yes | Usually omitted |
| Safe in a URL or filename | No | Yes |
| Used by | Email (MIME), data: URLs, HTTP Basic auth, PEM files | JWTs, OAuth PKCE, WebAuthn, URL and cookie tokens |
Use whichever the thing you are talking to expects. If the value travels in a URL, a filename or a cookie and you get to choose, use Base64URL without padding.
How Base64 works, in one paragraph
Base64 takes three bytes (24 bits) at a time and writes them as four characters of 6 bits each, drawn from an alphabet of 64 printable characters: A-Z, a-z, 0-9 and two more. The output is therefore four thirds the size of the input - 300 bytes become 400 characters. When the input does not divide by three, the last group is short, and = fills the gap so the output length is still a multiple of four:
a -> YQ==
ab -> YWI=
abc -> YWJjPadding carries no data. A decoder can tell how many bytes the last group holds from the length alone, which is why Base64URL can leave it out.
The two characters that differ
Most short ASCII text encodes identically in both alphabets, which is how the difference goes unnoticed in testing and then breaks in production. It only shows when a 6-bit group has the value 62 or 63. These five bytes hit both:
bytes fb ff fe 3e 3f
Base64 +//+Pj8=
Base64URL -__-Pj8And a piece of text whose encoding happens to contain them:
text subjects?_d=1&x=ÿ
Base64 c3ViamVjdHM/X2Q9MSZ4PcO/
Base64URL c3ViamVjdHM_X2Q9MSZ4PcO_You can reproduce both with the Base64 encoder and decoder, which has a URL-safe switch.
Why standard Base64 breaks in a URL
+in a query string is decoded as a space by form-style parsers, so the value arrives with a space where the plus was and no longer decodes to the same bytes./is the path separator. A token in a path segment gets split in two, and some servers and proxies normalise or reject encoded slashes.=separates keys from values in a query string. It usually survives, but it is one more character to escape.
You can keep standard Base64 and percent-encode it instead - + becomes %2B, / becomes %2F and = becomes %3D, as the URL encoder shows - but the result is longer and has to be decoded twice in the right order. Base64URL avoids the problem at the source.
Where each one is used
JSON Web Tokens are the most common place people meet Base64URL. Each of the three dot-separated parts is Base64URL without padding. This header:
{"alg":"HS256","typ":"JWT"}
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9is why so many tokens start with eyJ: that is {" followed by the start of a key. Paste a whole token into the JWT decoder to read all three parts. The same unpadded Base64URL is used for the OAuth PKCE code challenge and for WebAuthn credential IDs.
Standard Base64 is what the older protocols specify. HTTP Basic authentication sends username:password in standard Base64, with padding:
user:pa55word
Authorization: Basic dXNlcjpwYTU1d29yZA==That is an encoding, not protection: anyone who sees the header can decode it, so Basic auth is only safe over HTTPS. Email attachments (MIME) use standard Base64 broken into 76-character lines, data: URLs use it after ;base64,, and PEM certificate and key files use it between the -----BEGIN and -----END lines.
Converting between them
Because only the alphabet and padding differ, conversion is a text replacement and does not need decoding:
- Base64 to Base64URL: replace
+with-, replace/with_, and strip trailing=. - Base64URL to Base64: replace
-with+, replace_with/, and add=until the length is a multiple of four.
Many decoders are strict about one side of this. JavaScript's atob() accepts only the standard alphabet, so a JWT part has to be converted first; newer runtimes also offer Uint8Array.fromBase64() with an alphabet option. In Node.js, Buffer.from(text, "base64url") and buffer.toString("base64url") handle it directly. Python has base64.urlsafe_b64decode(), which requires the padding to be present, so add it back first.
A length that cannot be right is the usual sign of a value cut off in transit. A Base64 string can never have a length that leaves a remainder of one when divided by four. Decoding SGVsb in the tool reports:
Base64 is truncated — one character too many or too few.Common mistakes
- Treating Base64 as encryption. It has no key; anyone can reverse it. Do not use it to hide secrets.
- Base64-encoding text without fixing its character encoding first. Base64 works on bytes, so encode the text as UTF-8 and then to Base64. Otherwise accented characters and emoji come out differently on each side.
- Putting standard Base64 in a URL and testing only with values that happen to contain no
+or/. - Comparing two encodings of the same bytes as strings. A padded and an unpadded value, or a standard and a URL-safe one, are equal as data and unequal as text. Decode before comparing, or normalise both first.
- Encoding twice. If the decoded value still looks like Base64, it probably was.
