Base64 vs Base64URL

Published

In this guide

Standard Base64 uses + and / and usually = padding. Base64URL (RFC 4648 §5) uses - and _ and often omits padding so strings survive in URLs and JWTs.

Mixing the two causes decode errors. Convert deliberately before you paste into APIs or tokens.

Use our Base64URL encoder/decoder and JWT tool to verify values without uploading them.

Try these private tools next

The question “why does my Base64 fail to decode?” has a very common answer: it is actually Base64URL. Here is how to tell them apart and convert between them.

The differences

Base64 Base64URL
Character 62 + -
Character 63 / _
Padding = Required Usually omitted
Safe in URLs and file names No Yes
Typical use Data URIs, e-mail, JSON JWT, OAuth, URL parameters

Everything else — the algorithm and the other 62 characters — is identical, so most values look the same. A value is only distinguishable if it happens to contain one of the four special characters.

Symptoms of a mix-up

  • Python: binascii.Error: Incorrect padding or silently wrong bytes.
  • Java: IllegalArgumentException: Illegal base64 character 2d (2d is -).
  • .NET: FormatException: The input is not a valid Base-64 string.
  • JavaScript: atob throws InvalidCharacterError.
  • PHP strict mode returns false.

Conversion code

JavaScript

const urlToStd = (s) => s.replace(/-/g, '+').replace(/_/g, '/').padEnd(Math.ceil(s.length / 4) * 4, '=');
const stdToUrl = (s) => s.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');

Python

base64.urlsafe_b64decode(s + "=" * (-len(s) % 4))

PHP

base64_decode(strtr($s, '-_', '+/'), true);

Go

base64.RawURLEncoding.DecodeString(s)

Which one should you use?

  • Putting the value in a URL, cookie, file name or token: Base64URL without padding.
  • Embedding in HTML, CSS, JSON, XML or e-mail: standard Base64, because that is what data URIs and MIME require.
  • Designing an API: accept both on input, document exactly one on output.

Our Base64 decoder accepts both automatically. To convert explicitly, use the Base64URL encoder, the Base64URL decoder or normalization.

คำถามที่พบบ่อย

Which one do JWTs use?

JWT header and payload segments use Base64URL without padding.

How do I convert Base64 to Base64URL?

Replace + with -, / with _, and strip = padding. Or use the Base64URL tools on this site.

เครื่องมือและคู่มือที่เกี่ยวข้อง

ความคิดเห็น (0)

มีคำถาม ไอเดีย หรือพบข้อผิดพลาดใน “Base64 vs Base64URL” ใช่ไหม? แสดงความคิดเห็น ให้คะแนนเครื่องมือ หรือตอบกลับผู้อื่นได้เลย

ยังไม่มีความคิดเห็น มาเป็นคนแรกที่แสดงความคิดเห็นสิ!

แสดงความคิดเห็น

ให้คะแนนเครื่องมือนี้:
Esc
↑ ↓ เลื่อน↵ เปิดEsc ปิด