← All blog posts

Common mistakes when calculating base64

WhiteUI · A plain-English guide to the Base64 Encoder Decoder

Base64 is a short calculation, and most of the ways it goes wrong are small slips with large consequences: a missing character, the wrong alphabet, or a wrong assumption about what the output means. This guide collects the mistakes people actually make, shows what each one does to the result, and gives the fix for each — check them yourself on the Base64 Encoder Decoder.

Three mistakes: decoding SGVsbG8 without its equals sign fails, plus and slash break in web addresses, and Base64 is not encryption
Three of the six mistakes to avoid: dropping the padding, using + and / in a web address, and treating Base64 as a secret.

1. Dropping the padding

The commonest mistake is copying an encoded string without its equals signs. Hello encodes to SGVsbG8=, and the final = is part of the data, not decoration. Drop it and you get SGVsbG8, which is seven characters long, and most decoders refuse it with an incorrect padding error, because the length is not a multiple of four and the decoder cannot tell how many bytes the last group held. The fix is simple: always copy the whole string, equals signs included, and if a string you received fails to decode, check the end first. A single missing = is the cause more often than any other error.

2. Using the standard alphabet in a web address

The standard alphabet contains plus and slash, and both are special characters in a web address: plus can be read as a space, and slash separates the parts of the address. The bytes FB FF FE encode to +//+ in standard Base64, which will break if it is pasted into a web address as it is. The URL-safe variant replaces plus with a minus and slash with an underscore, giving -__-, which travels safely. The fix is to know which alphabet the system you are feeding expects, and to use the URL-safe version whenever the result will appear in an address, a file name or a token. Mixing the two alphabets is a silent error: the string looks valid, but it decodes to the wrong bytes.

3. Counting characters instead of bytes

Base64 works on bytes, and for plain English the two counts match, which is why this mistake hides so well. The word Café looks like four characters, but é takes two bytes in UTF-8, so Café is five bytes and encodes to Q2Fmw6k=, not to the four-byte result a character count would predict. The rupee sign is one character and three bytes, encoding to 4oK5, and an emoji is one character and four bytes, encoding to 8J+YgA==. The fix is to count bytes, and to make sure the text is read as UTF-8 before encoding. If your encoded output is longer than you expected, the extra bytes are almost always hiding in a non-English character.

4. Encoding the same text twice

Encoding an already-encoded string is easy to do by accident, especially in code that runs the same step more than once. The word hello encodes to aGVsbG8=, and encoding that string again gives YUdWc2JHOD0=, which decodes back to aGVsbG8=, not to hello. The fix is to decode once and look: if the result is itself a Base64-looking string, it was probably encoded twice. Keep track of which strings are plain text and which are already encoded, and encode each piece of data exactly once.

5. Treating Base64 as encryption

This is the most dangerous mistake on the list, because it feels like security and is not. Base64 is a transport format: anyone who sees the string can decode it in a second, with no key, no password and no special tool. A password stored as Base64 is a password stored in plain text with extra steps. The numbers make the point clearly: 12345 encodes to MTIzNDU=, and decoding MTIzNDU= gives 12345 back to anyone who tries. The fix is to use real encryption for anything secret, and to treat Base64 only as a way of carrying data safely through text-only channels.

6. Forgetting that case matters

Base64 is case-sensitive in both directions. Hello encodes to SGVsbG8=, while hello with a small h encodes to aGVsbG8=, a completely different string. Changing the case of a single character in an encoded string changes the byte it stands for, so the decoded text comes out wrong or the decode fails outright. The fix is to copy encoded strings exactly, character for character, and never to tidy them up by changing the case or removing characters that look unimportant.

Frequently asked questions

Why does my Base64 string fail to decode?

Usually the padding was dropped. SGVsbG8 without its final = is 7 characters long, and most decoders refuse it with an incorrect padding error.

Why does Base64 break in a web address?

The standard alphabet uses + and /, which are special in addresses. Use the URL-safe variant, where + becomes - and / becomes _, giving -__- instead of +//+.

Is Base64 the same as encryption?

No. Anyone can decode a Base64 string with no key, so it hides nothing. A password stored as Base64 is a password stored in plain text.

Why is my encoded output longer than I expected?

You probably counted characters instead of bytes. Café is 4 letters but 5 bytes in UTF-8, and an emoji is 1 character but 4 bytes.

More guides on the Base64 Encoder Decoder

Try it yourself

Try the Base64 Encoder Decoder — free, in your browser.

Open the Base64 Encoder Decoder