Base64URL

Đã xuất bản

Trong hướng dẫn này

Base64URL is the URL-safe profile of Base64: - and _ instead of + and /, padding often omitted.

It is required in JWT, many OAuth tokens, and filenames. Standard Base64 will break in query strings.

Encode and decode Base64URL here, then open the JWT decoder for real tokens.

Thử các công cụ riêng tư tiếp theo

Base64URL (“base64 with URL and filename safe alphabet”, RFC 4648 section 5) is standard Base64 with two characters replaced:

Value Standard Base64URL
62 + -
63 / _

In URLs, + can mean a space and / separates path segments, and both are invalid in file names on some systems. - and _ have no special meaning anywhere.

Padding

The RFC allows omitting = when the length is known from context, and almost every user of Base64URL does so (because = would need to be percent-encoded as %3D in URLs). Decoders should therefore accept values with and without padding.

Where it is used

  • JSON Web Tokens (JWT): header, payload and signature are unpadded Base64URL. Decode one with the JWT decoder.
  • WebAuthn / passkeys, OAuth PKCE code challenges, and many API keys.
  • URL query parameters and short IDs (for example YouTube video IDs use the same alphabet).
  • File names generated from hashes.

Converting

From → to Steps
Base64 → Base64URL replace + with -, / with _, remove =
Base64URL → Base64 replace - with +, _ with /, append = until the length is a multiple of 4

Example: +/+/ ⇄ -_-_; SGk (Base64URL) → SGk= (Base64).

Use the Base64URL encoder and decoder, or convert existing values with normalization.

Code

// JavaScript
const toB64Url = (b64) => b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
# Python
base64.urlsafe_b64encode(data).rstrip(b"=")

More examples: JavaScript, Python, PHP, Go.

Câu hỏi thường gặp

Is padding required in Base64URL?

RFC 4648 allows omitting padding. JWT almost always omits it; some libraries still accept =.

Can I use standard Base64 in a URL?

Only if you percent-encode + / and =. Prefer Base64URL instead.

Công cụ và hướng dẫn liên quan

Bình luận (0)

Có câu hỏi, ý tưởng hoặc phát hiện lỗi trong “Base64URL”? Hãy để lại bình luận, đánh giá công cụ hoặc trả lời người khác.

Chưa có bình luận nào. Hãy là người đầu tiên chia sẻ ý kiến!

Để lại bình luận

Đánh giá công cụ này:
Esc
↑ ↓ di chuyển↵ mởEsc đóng