🌐 English

Home › Guides

What Base64 is and when to use it

Base64 is a notation that rewrites arbitrary bytes using 64 safe characters. It is often mistaken for a kind of encryption, but it hides nothing. Knowing how it works, how much it grows and which variants exist saves a lot of confusion.

Encoding is not encryption

Base64 is a publicly specified notation with no key. Anyone can reverse it using the same rules, so wrapping a password or personal data in Base64 does not hide it in any meaningful way.

Encryption makes data unreadable without a secret key, while encoding only changes the shape of data so another system can carry it. The goals are different. If you need protection, use real encryption or hashing, and use Base64 only to write the result as text.

How it works and how much it grows

Take the input three bytes at a time, which is 24 bits. Split those 24 bits into four groups of 6 bits, and map each group to one character of a 64-character alphabet (A-Z, a-z, 0-9, +, /). Three bytes become four characters, so the output is about one third larger than the input.

If the input length is not a multiple of three, one or two = characters are appended as padding so the output length is a multiple of four. A final group of one byte gets ==, and a group of two bytes gets =.

The URL-safe variant

In the standard alphabet, + and / have special meanings in addresses and file paths. A URL-safe variant replaces them with - and _, and it frequently drops the trailing = padding as well.

The parts of a login token (JWT) are the best-known example. Feeding a URL-safe string to a standard decoder can raise an error or give a wrong result, so check which flavor you have before decoding.

When to use it and when not to

Use Base64 when you must move binary data through a channel that only handles text safely. Email attachments, small images embedded directly in a page as data URLs, and certificates or key files stored in text-based settings are typical cases.

For large files it is a poor choice because the size grows by a third and processing is slower; send the file as it is whenever you can. Remember that it offers no compression and no security.

When decoding fails: what to check

Most decoding errors come from a truncated string, the wrong variant, or stray whitespace. Checking whether the length is a multiple of four and whether any illegal characters appear usually narrows the cause quickly.

Copying often drags along line breaks, leading or trailing spaces, or quotation marks. If the result looks like random symbols rather than text, the original may be binary data such as an image or a compressed file, not text at all.