Base64 explained: what it is and when not to use it
Base64 is not encryption and it is not compression. It is a way to carry arbitrary bytes through a channel that only accepts text, and once you internalise that, most of the confusion about it disappears.
What the encoding actually does
The encoder takes three bytes (24 bits) at a time and splits them into four groups of six bits. Each six-bit value maps to one of 64 printable characters: A–Z, a–z, 0–9, plus + and /. When the input does not divide evenly, the output is padded with =, which is why encoded strings often end with one or two equals signs.
The cost: roughly 33 percent
Four characters carry three bytes, so anything you encode grows by about a third before the container overhead. A 300 KB image becomes a 400 KB string. That is fine for a small icon inlined in CSS and a bad idea for a photo gallery.
- Good fit — small icons as data URIs, short binary blobs inside JSON, tokens that must survive a text-only transport.
- Bad fit — large media, anything you could send as multipart form data, or anything you are tempted to call "protected".
It does not protect anything
Anyone can decode a Base64 string with one command. If you need confidentiality, you need encryption, and if you need integrity you need a signature. Treating Base64 as obfuscation leads to credentials sitting in a configuration file in plain sight with a thin coat of paint.
Text and UTF-8 are not the same thing
The specification works on bytes, not characters. A Chinese or accented character occupies more than one byte in UTF-8, so a naive encoder that treats each character as one byte produces output that decodes into mojibake. A correct implementation encodes the UTF-8 byte sequence, and decoding reverses it.
This is the most common bug we see: a string looks right in English and turns into garbage the moment someone types an emoji.
Data URIs, the everyday case
A data URI embeds the content directly in the document: data:image/png;base64,iVBORw0KGgo.... It saves an HTTP request, which helps for a 1 KB icon and hurts for anything large, because the encoded content cannot be cached separately and blocks rendering while it downloads.
Encoding and decoding safely
The Base64 tool handles UTF-8 correctly in both directions and can turn an image file into a data URI. Because the conversion happens in the browser, the file you drop in is never uploaded — useful when the asset belongs to a client who has not signed off on third-party processing.
Previous: Five JSON mistakes that break a parser
Next: None