Secrets and encoding

Garbled text after Base64 decoding: UTF-8 troubleshooting

Base64 decoding returns bytes. A separate text decoder interprets those bytes using a character encoding. If UTF-8 bytes are treated as individual characters, café can become café. Base64 Encode and Decode encodes UTF-8 and requires valid UTF-8 on decode.

Text is encoded to bytes, represented in Base64, then decoded back to the original text.
The byte-to-text step must use the same character encoding as the producer.

Follow café through the byte representation

The UTF-8 bytes for café are 63 61 66 c3 a9 in hexadecimal. Their Base64 representation is Y2Fmw6k=. The final character occupies two bytes; interpreting each byte as a character produces the visible mojibake café.

Use the following values as a controlled test. They let you distinguish a damaged Base64 string from a correct byte sequence decoded with the wrong character encoding. A screenshot of garbled text alone cannot establish which stage failed.

A synthetic round trip
StageValue
Original textcafé
UTF-8 bytes, hex63 61 66 c3 a9
Base64Y2Fmw6k=
Incorrect byte-as-character displaycafé

Decode bytes before interpreting text

The browser atob function produces a binary string. Its character codes represent byte values; the result is not automatically a correctly decoded Unicode string. Feed those values into TextDecoder when the producer used UTF-8.

This snippet handles the known standard Base64 example. Production code should validate the expected alphabet and data contract. It does not provide a general file decoder or a way to infer the encoding of arbitrary bytes.

const binary = atob("Y2Fmw6k=");
const bytes = Uint8Array.from(binary, ch => ch.charCodeAt(0));
const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
console.log(text); // café

// Encoding the same text:
const input = new TextEncoder().encode("café");
btoa(Array.from(input, b => String.fromCharCode(b)).join(""));
// Y2Fmw6k=

Preserve the original bytes while investigating

If PageUtil reports that decoded bytes are not UTF-8, the producer may have used another encoding, supplied binary content, or truncated the data. Do not force replacement characters and assume the result is fixed. Compare a known-good sample with the producing application’s charset settings.

If text was already corrupted before it was Base64-encoded, decoding correctly will preserve that corruption. Find the earliest step where the expected UTF-8 bytes differ. Repeated “repair” conversions can make the original evidence unrecoverable.

For large files or non-text payloads, use a local byte-oriented tool appropriate to the format. PageUtil’s text conversion stays on your device, but it intentionally rejects invalid UTF-8 rather than guessing another encoding.

References

Use the linked specifications and product documentation to check behavior in your own environment.

Continue troubleshooting

Related tools

Frequently asked questions

Does Base64 specify the character encoding?

No. It represents bytes; the surrounding format must specify how bytes become text.

Why does PageUtil reject binary Base64?

Its text decoder requires valid UTF-8 rather than treating arbitrary bytes as readable characters.

Can correct decoding repair text corrupted earlier?

No. Correct decoding reproduces the stored bytes, including any earlier corruption.

Need help with a tool or found a bug?

Report an issue directly to engineering. No accounts required — all client data remains private on your device.

Report an Issue →