DEV Community

Usama Farooq
Usama Farooq

Posted on Edited on Originally published at toolsofweb.com

How Base64 Encoding Works Under the Hood

How Base64 Encoding Works Under the Hood

Base64 turns any binary data — a photo or a file — into plain text made of 64 characters: A–Z, a–z, 0–9, + and /. It regroups the data's bytes into 6-bit chunks and gives each chunk a letter or digit, at the cost of output about a third bigger than the original — and it is encoding, not encryption.

Try the free Base64 Converter — encode or decode text in your browser with ToolsOfWeb's Base64 Converter. Free, no sign-up, nothing is uploaded.

What is Base64 encoding, really?

Base64 is an encoding scheme that represents any bytes as text using only 64 safe ASCII characters, so binary data can travel through systems that only handle text. The modern standard is RFC 4648, published by the IETF in October 2006.

Email was the original reason it exists: early email could only carry plain text, so attachments had to be converted into text first. RFC 4648 defines the alphabet, the padding rules and the URL-safe variant.

The key idea is simple: normal text systems choke on raw bytes. A byte can hold any value from 0 to 255, including control characters that break email servers, URLs and JSON. Base64 sidesteps the whole problem by re-expressing those bytes with characters that are safe everywhere — letters, digits, and two symbols. The result looks like gibberish, but it is fully reversible: decoding gives back the exact original bytes.

How does Base64 turn bytes into text?

Base64 works by taking the input three bytes at a time — 24 bits — and splitting those 24 bits into four groups of 6 bits, then looking up each 6-bit group in a 64-character alphabet. Three bytes in, four characters out, every time.

Each group is a number from 0 to 63, and the alphabet maps 0–25 to A–Z, 26–51 to a–z, 52–61 to 0–9, 62 to + and 63 to /.

Take the word "Man". Its three bytes are M (77), a (97) and n (110), or in binary 01001101 01100001 01101110. Split into 6-bit groups you get 010011, 010110, 000101 and 101110 — the numbers 19, 22, 5 and 46. Look those up in the alphabet and you get T, W, F and u. So "Man" encodes to "TWFu". Every Base64 string you have ever seen is built from exactly this regrouping, one 3-byte block at a time.

Why does Base64 output end with = padding?

Base64 output ends with = padding whenever the input length is not a multiple of three bytes, because the encoder always works in 3-byte blocks and pads the final block. One leftover byte produces two characters plus ==, and two leftover bytes produce three characters plus =.

The padding tells the decoder exactly how many real bytes the last block held, so it can strip the filler and restore the original.

A one-letter example makes it concrete: "M" is a single byte, so it encodes to two characters, "TQ", followed by "==". "Ma" is two bytes, so it becomes "TWE=". Without padding, the decoder could not tell whether a short final group meant one byte or two. Some modern variants, like Base64url in web tokens, drop the padding because the length is known from context — but classic Base64 always keeps it.

What is Base64url, and how is it different?

Base64url is the URL-safe variant of Base64: it swaps + for - and / for _, and usually drops the = padding, because + , / and = all have special meanings inside URLs. The encoding math is identical — same 6-bit groups, same 3-to-4 regrouping — only three characters change.

You meet it most often in JSON Web Tokens, where each token part is Base64url-encoded.

This matters in practice. Paste standard Base64 into a URL and the + becomes a space and the / starts a new path segment; the data arrives corrupted. That is why APIs and tokens use the URL-safe alphabet, and why a good converter offers both modes. If you want the full story on tokens, read how JWTs work under the hood — every JWT you have seen is three Base64url strings joined by dots.

Is Base64 encryption?

No. Base64 is encoding, not encryption: it changes the shape of data without any secret key, so anyone who recognises the format can decode it instantly. Encoding exists to make data transportable; encryption exists to make data unreadable without a key. Treating Base64 as protection is one of the most common security mistakes on the web.

You will still see it misused that way — passwords or tokens "hidden" as Base64 strings in code, config files or URLs. Decoding takes one click, so it protects nothing. If a secret needs protecting, encrypt it or hash it properly; encoding is only the envelope. The honest rule is simple: Base64 answers "how do I carry this data somewhere text-only?", never "how do I keep this data secret?".

FAQs

Can I decode Base64 without installing anything?

Yes. Paste the string into a browser tool like the free Base64 Converter and it decodes on your device — nothing is uploaded. Developers can also use built-in functions: atob() and btoa() in the browser, or the base64 module in Python. If it ends in = padding and uses only letters, digits, + and /, it is almost certainly Base64.

Why is Base64 output about a third bigger than the input?

Because 3 bytes (24 bits) become 4 characters, and each character is stored as a full byte — so every 3 bytes of data need 4 bytes of text, a 33% overhead. That size cost is the price of text safety, and it is why Base64 suits small payloads like tokens, not whole videos.

Is Base64 safe for passwords?

No, never store or send a password as plain Base64 — anyone can decode it in a second. Passwords should be hashed with a slow, salted algorithm, never merely encoded. Base64 is fine for carrying a hash or a token that is already protected, but on its own it is not a security measure at all.

What is the difference between Base64, Base32 and Base16?

They are siblings from the same standard, RFC 4648, differing only in alphabet size: Base16 uses 16 characters (0–9, A–F) and doubles the size, Base32 uses 32 characters (A–Z, 2–7) and grows output by 60%, and Base64 uses 64 characters and grows it by 33%. Smaller alphabets survive stricter systems — Base32 avoids characters people confuse when reading codes aloud.

Does Base64 work with images and files?

Yes — any bytes can be Base64-encoded, including images, PDFs and audio. That is how small images get embedded directly in web pages as data URIs, and how email attachments travel as text. For larger files it is wasteful because of the 33% overhead, so it is best for small assets — see the step-by-step Base64 guide.

Try the free Base64 Converter

Open the Base64 Converter and encode or decode text in your browser — free, no sign-up, your data never leaves your device.

If you liked this explainer, read how JWTs work under the hood and the Base64 encode and decode guide. For a related encoding, try the URL Encoder & Decoder.

Top comments (0)