DEV Community

Sanjaya Sahu
Sanjaya Sahu

Posted on

Base64 Encoding: 6 Mistakes That Corrupt Your Data

Base64 looks trivial until a token, image or payload arrives corrupted. Here are the mistakes I see most often.

1. Treating Base64 as encryption

Base64 is an encoding, not security. Anyone can decode it in one line. Never use it to hide passwords or secrets.

2. Unicode and btoa

btoa("héllo ✓") throws in browsers because it only accepts Latin-1. Encode to UTF-8 bytes first:

const bytes = new TextEncoder().encode("héllo ✓");
const b64 = btoa(String.fromCharCode(...bytes));
Enter fullscreen mode Exit fullscreen mode

3. Mixing standard and URL-safe alphabets

Standard Base64 uses + and /. URL-safe uses - and _. JWTs use the URL-safe form without padding, so a plain atob on a JWT segment can fail. Swap the characters back and re-add = first.

4. Forgetting padding

Base64 output length must be a multiple of 4. Missing = characters make strict decoders fail. Pad the string until its length is divisible by 4.

5. Line breaks in the data

MIME-style Base64 wraps lines at 76 characters. Strip \r and \n before decoding if your decoder is strict.

6. Embedding huge files as data URIs

Base64 adds about 33% size overhead and blocks caching. Fine for tiny icons, bad for large images.

Check it quickly

Paste your string into the browser tool below; it runs entirely client-side, so nothing is uploaded.

Try it: Base64 Decode and Base64 Encode

Related: JWT Decoder, URL Encode and JSON Formatter. All 590+ tools at webdevtoolbox.com run client-side.

Top comments (0)