DATA REPRESENTATION CHECKS

Checking Base64 alphabets before choosing a decoder

Standard Base64 and URL-safe Base64 swap two characters, plus and slash for hyphen and underscore. This guide uses the text ??? to show the difference and explains which decoder fits which source.

How three bytes become four characters

Base64 reads input three bytes at a time, which is 24 bits, and cuts them into four groups of six bits. Each six-bit value from 0 to 63 selects one character from a 64-character alphabet. The text ??? is three question marks, each the byte 3F in UTF-8, so the 24 bits are 00111111 repeated three times. The groups are 001111, 110011, 111100 and 111111, which are the values 15, 51, 60 and 63. In the standard alphabet those map to P, z, 8 and the slash, giving Pz8/. The mapping is defined in RFC 4648, the 2006 document that specifies Base16, Base32 and Base64. The same bytes produce a different last character when the alphabet changes, which is the whole point of the example.

The two symbols that differ

Values 0 to 51 map to the letters A to Z and a to z, and 52 to 61 map to the digits 0 to 9. Only 62 and 63 differ. The standard alphabet from RFC 4648 section 4 uses plus for 62 and slash for 63. The URL and filename safe alphabet in section 5 uses hyphen for 62 and underscore for 63. Everything else, including the equals sign used for padding, matches. Because the first 62 symbols are shared, many encoded strings are identical in both forms and the difference shows only when a value of 62 or 63 appears. The text ??? triggers it because its final group is 111111, which is 63. A string of three greater-than signs triggers the other symbol: it encodes to Pj4+ in the standard form and Pj4- in the URL-safe form.

Why a URL-safe variant exists

The standard alphabet has two characters with special meaning in addresses. A slash separates path segments, so a Base64 value containing one can split a path or be mistaken for a directory. A plus sign in a query string is read by form decoding as a space, which silently corrupts the value. File systems add their own limits on slashes. The URL-safe alphabet replaces both with characters that need no percent-encoding, so a token can sit in a path or query without extra escaping. JSON Web Tokens, defined in RFC 7519 and signed under RFC 7515, use this variant for their three dot-separated parts, and the encoding there omits the trailing equals signs. Many web APIs follow the same convention for identifiers and cursors.

What the three cases show

The first case encodes ??? with the standard encoder and returns Pz8/, ending in a slash. The second encodes the same text with the URL-safe encoder, padding off, and returns Pz8_, ending in an underscore. The third decodes Pz8_ with the URL-safe decoder and gets ??? back, which confirms the round trip. Three bytes need no padding, so the padding setting has no visible effect here. The matching decoder is the point. Pz8_ is valid for the URL-safe alphabet and carries a character that the standard alphabet does not contain, so a strict standard decoder would reject it as invalid. A lenient library may accept both, but a strict one will not.

Padding is a separate decision

Base64 output length is a multiple of four characters. When the input length is not a multiple of three, the encoder adds one or two equals signs to complete the last group. The single byte 3F, one question mark, encodes to Pw== in the standard form. The URL-safe form often drops the padding, giving Pw, since the length of the original data can be worked out from the length of the unpadded string. RFC 4648 allows omitting padding when the specification using it says so, and some do while others require it. The alphabet and the padding rule are independent settings, which gives four combinations in practice. A decoder that wants padding will reject Pw, and one that forbids it will reject Pw==.

Choosing the decoder from the field definition

The right decoder depends on how the producing system defines the field, and the characters in the value only hint at it. Seeing a hyphen or underscore tells you it is URL-safe, and seeing a plus or slash tells you it is standard, but a short value may contain neither. Look for the specification: a JWT segment is URL-safe without padding, a data URI uses the standard alphabet with padding, and MIME email bodies under RFC 2045 use the standard alphabet with line breaks every 76 characters. Converting between the two alphabets is a simple character swap, but only when you know which side the data started on. Replace the symbols in a value that already uses the other alphabet and you will change bytes, not just spelling.

Text bytes come first

These examples encode text, so the tool first turns the characters into UTF-8 bytes and then applies Base64. For ASCII characters each character is one byte, which makes the arithmetic above straightforward. Other characters can take two to four bytes, and the encoded form changes accordingly. A different character encoding would give different bytes and a different Base64 string for the same visible text. The guide examines the printable text path only. Encoding arbitrary binary data such as images or keys follows the same alphabet rules but is not what these controls inspect. The decoder strictly validates the alphabet, padding and unused trailing bits, so a damaged value produces an error and does not decode silently into the wrong bytes.

Longer inputs and the length rule

Encoded length follows a simple formula. With padding, the output has 4 times the ceiling of n divided by 3 characters for n input bytes, so 3 bytes give 4 characters, 4 bytes give 8 and 6 bytes give 8. That is about a 33 percent size increase. Without padding, the length is the ceiling of 4n divided by 3. A string whose unpadded length leaves a remainder of 1 when divided by 4 can never be valid, since a single leftover character carries only six bits and cannot complete a byte. Knowing this rule helps you spot a truncated token, such as a signature cut off by a copy and paste, before you waste time on the decoder.

Keeping the label with the value

The practical habit is to record the alphabet and padding policy next to every Base64 value you store or send. A field named token_b64url with no padding tells the next reader exactly which decoder to pick. A bare string called data invites guesses. When debugging a failure, check three things in order: whether the string contains plus, slash, hyphen or underscore characters, whether its length is a multiple of four, and whether the decoder expects padding. This worksheet verifies one pair of alphabets on one short input. It does not test arbitrary binary content or recommend one variant over the other.

Reproduce the checks

Standard alphabet

Open the Base64 Encoder Decoder and enter:

Text or encoded input
???
Calculation mode
encode

Expected output:

Text

Pz8/

The final symbol is a slash.

URL-safe alphabet

Open the Base64Url Encoder Decoder and enter:

Text or encoded input
???
Calculation mode
encode
Include Base64 padding
false

Expected output:

Text

Pz8_

The corresponding symbol is an underscore.

URL-safe decode control

Open the Base64Url Encoder Decoder and enter:

Text or encoded input
Pz8_
Calculation mode
decode
Include Base64 padding
false

Expected output:

Text

???

The matching decoder returns the original text.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks