Base64url encode and decode
Runs in your browserEncode and decode Base64url, convert between Base64, Base64url and hex, fix missing padding and find invalid characters.
Base64 vs Base64url
| Base64 | Base64url | |
|---|---|---|
| Characters 62 and 63 | + and / | - and _ |
| Padding | = up to a multiple of 4 | usually left out |
| Used in | e-mail, data: URLs, PEM files, Basic auth, Kubernetes secrets | JWT, JWK, PKCE, URLs and file names |
| Standard | RFC 4648 §4 | RFC 4648 §5 |
Both encode the same bytes. Converting is just swapping two characters and adding or removing the padding. Standard Base64 breaks in URLs because + means a space in a query string and / separates paths.
Base64url without a library
Buffer.from("hello").toString("base64url"); // aGVsbG8
Buffer.from("aGVsbG8", "base64url").toString(); // hello// Modern browsers
new Uint8Array([104, 105]).toBase64({ alphabet: "base64url", omitPadding: true });
// Everywhere
btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");import base64 base64.urlsafe_b64encode(b"hello").rstrip(b"=") base64.urlsafe_b64decode(s + "=" * (-len(s) % 4)) # add the padding back
rtrim(strtr(base64_encode($data), '+/', '-_'), '='); base64_decode(strtr($s, '-_', '+/'));
printf '%s' hello | base64 | tr '+/' '-_' | tr -d '=' # basenc (GNU coreutils 8.31+) keeps the padding printf '%s' hello | basenc --base64url | tr -d '='
Questions about the base64url
Why does my Base64 decoder fail on a JWT?
JWTs use Base64url: - and _ instead of + and /, and no padding. Most Base64 decoders reject those characters. This tool reads both.
Is the padding required?
In standard Base64 yes, in Base64url usually not. RFC 7515 (JWT) forbids it. The padding is fully determined by the length, so it can always be added back.
What does “one character over” mean?
Base64 groups are 4 characters for 3 bytes. A length that leaves one character after the last full group can’t come from any bytes, so the string was cut or something was added.