Base64url encode and decode

Runs in your browser

Encode and decode Base64url, convert between Base64, Base64url and hex, fix missing padding and find invalid characters.

Try:
The difference

Base64 vs Base64url

Base64Base64url
Characters 62 and 63+ and /- and _
Padding= up to a multiple of 4usually left out
Used ine-mail, data: URLs, PEM files, Basic auth, Kubernetes secretsJWT, JWK, PKCE, URLs and file names
StandardRFC 4648 §4RFC 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.

In your code

Base64url without a library

Node.js
Buffer.from("hello").toString("base64url");      // aGVsbG8
Buffer.from("aGVsbG8", "base64url").toString();      // hello
Browser
// Modern browsers
new Uint8Array([104, 105]).toBase64({ alphabet: "base64url", omitPadding: true });
// Everywhere
btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
Python
import base64
base64.urlsafe_b64encode(b"hello").rstrip(b"=")
base64.urlsafe_b64decode(s + "=" * (-len(s) % 4))   # add the padding back
PHP
rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
base64_decode(strtr($s, '-_', '+/'));
Command line
printf '%s' hello | base64 | tr '+/' '-_' | tr -d '='
# basenc (GNU coreutils 8.31+) keeps the padding
printf '%s' hello | basenc --base64url | tr -d '='
FAQ

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.

Searches the list of pages on this site. What you type stays in your browser.