Small SEO Tools

Why Truncated Base64 Can Still Decode

A Base64 value can lose its last four characters and still decode to readable text. If the result is shorter than expected, compare the decoded byte count with the count recorded before the transfer. A successful decode tells you the received value can be read as Base64.

Decode the complete and shortened values

Imagine a message that says Meet at noon B2, where B2 is the meeting room. The sender records the message length as 15 UTF-8 bytes, including the spaces. The standard Base64 value is below.

TWVldCBhdCBub29uIEIy
  1. Open the Base64 Encoder and Decoder, choose Decode Base64 to text, and paste the complete value.
  2. Press Convert. The result is Meet at noon B2 and Decoded bytes shows 15 B.
  3. Remove the final IEIy from the input and press Convert again. The remaining input is TWVldCBhdCBub29u. This time the result is Meet at noon and Decoded bytes shows 12 B.

Both inputs decode successfully. The second result reads like a complete sentence, but it has lost the room number and the space before it.

Why the missing group passes a format check

Standard Base64 represents every three full input bytes with four encoded characters. In this example the original five groups become four. Both versions end at a group boundary, so neither needs padding. The shorter value also happens to be the standard encoding of the shorter message. RFC 4648 Base64 rules

Strict last-group handling in Uint8Array.fromBase64() checks for a correctly formed ending, including unused bits in a padded group. The shortened example ends with a full group and passes that check. The decoder has no expected message length to compare against. ECMAScript Base64 decoding algorithm

Compare the byte count recorded before transfer

  1. Record the size of the original text in UTF-8 bytes before encoding or copying it. If you have the original string in JavaScript, new TextEncoder().encode(originalText).length gives its UTF-8 byte count.
  2. Decode the received value and compare Decoded bytes with that original size. In the example, the mismatch of 12 and 15 bytes gives you a concrete reason to check the export or request the original value again.
  3. When the sizes match, compare the decoded text with the source if you have it. A change from B2 to B3 leaves the byte count unchanged.

Use bytes consistently on both sides. For example, piñata has six characters in this spelling and takes seven UTF-8 bytes. A character count would give the wrong expected size for that text. WHATWG TextEncoder specification

Trace where the bytes went missing

Compare the received value with the sender’s export before editing it. If the export has the full value and a copied field holds the shorter one, check the handoff between them. For example, a copy from a table preview may stop at the visible text. Copy from the full value or export instead and repeat the byte and text checks.

If the original value is unavailable, ask the sender to resend it with the expected byte count. Adding padding to the shortened example cannot recreate the missing room number. For a repeatable encode and decode test with non-ASCII text, see how to round-trip UTF-8 text through Base64.