What Is Base64? Encoding, Padding & When to Use It
Binary data can't travel through text-only systems β so we spell it out in 64 safe characters. Learn how three bytes become four characters, what the = padding means, and why Base64 is not encryption.
What you will learn
- Explain why binary data needs a text encoding at all
- Work out how 3 bytes become 4 Base64 characters
- Read padding (=, ==) and know when it appears
- Tell Base64 from Base64URL β and know which one a JWT uses
- Know where Base64 belongs (and where it doesn't)
- What Base64 is β and why it exists
- How it works, step by step
- Base64 vs Base64URL
- Where you actually meet Base64
- What Base64 is not
- Common mistakes
- Try it: Base64 Encoder
- Frequently asked questions
What Base64 is β and why it exists
Many systems only handle text: email bodies, JSON, URLs, XML, source code. Binary data β an image, a file, a cryptographic signature β contains bytes that these systems would corrupt or reject (a zero byte, a control character, a byte that isn't valid UTF-8).
Base64 is a way to spell arbitrary bytes using only 64 safe characters: AβZ, aβz, 0β9, plus + and / (with = for padding). The result is plain ASCII text that survives email, JSON and copy-paste untouched. It is an encoding, not encryption: anyone can decode it, by design.
The name gives the game away: 64 characters means each character carries 6 bits of information (26 = 64). Three bytes are 24 bits; 24 Γ· 6 = 4 β so every 3 bytes of input become exactly 4 characters of output. That 4:3 ratio is why Base64 output is always about 33% larger than the input.
How it works, step by step
Take the word Man β bytes M (77), a (97), n (110):
M = 01001101
a = 01100001
n = 01101110
Joined: 010011 010110 000101 101110
T W F u
Result: TWFuThe 24 bits are split into four 6-bit groups, and each group indexes into the alphabet (A=0, B=1, β¦ Z=25, a=26, β¦ 0=52, +=62, /=63). Decoding is the reverse: look each character up, reassemble the bits, split back into bytes.
When the input isn't a multiple of 3 bytes, the last group is padded with zero bits and the output is padded with = so the length stays a multiple of 4:
| Input | Bytes | Output | Padding |
|---|---|---|---|
Man | 3 | TWFu | none |
Ma | 2 | TWE= | one = |
M | 1 | TQ== | two = |
The padding isn't decoration: it tells the decoder exactly how many bytes the last group really held. Some systems (notably JWT) drop the padding and let the decoder work it out from the length β valid Base64URL, but not strict Base64.
Base64 vs Base64URL
| Characters 62 & 63 | Padding | Used in | |
|---|---|---|---|
| Base64 (standard) | + and / | = kept | Email (MIME), data URIs, XML |
| Base64URL | - and _ | usually dropped | JWT, URLs, filenames |
The URL variant exists because + means βspaceβ in query strings and / is a path separator β standard Base64 in a URL would be mangled. Mixing the two alphabets is a classic bug: decoding Base64URL with a standard decoder fails on the first -. If your code handles tokens (JWTs) and files (MIME) in the same place, keep the two decoders separate.
Where you actually meet Base64
- Email attachments (MIME). Email was designed for text; attachments are Base64-encoded into the message body. That is why a 10 MB attachment makes an email roughly 13 MB.
- Data URIs.
data:image/png;base64,iVBORw0β¦embeds a small image directly in HTML or CSS β one less request, at the cost of 33% extra bytes. Good for tiny icons, terrible for photos. - JWTs. The header and payload of a JSON Web Token are Base64URL β which is why anyone can decode them (see our JWT guide).
- HTTP Basic Auth.
Authorization: Basic dXNlcjpwYXNzis justuser:passin Base64 β obscured, not protected. Without HTTPS it is trivially readable. - Embedding binary in JSON or config. Small keys, certificates and thumbnails travel as Base64 strings because JSON has no binary type.
What Base64 is not
It is not encryption. There is no key and no secret β decoding is a lookup table anyone can apply. Treating Base64 as βhidingβ data is a security bug, not a feature.
It is not compression. Output is always larger β about 33% larger, plus padding. If size matters, compress first (gzip), then encode if you must; never encode hoping to save space.
It is not a hash. A hash is one-way and fixed-size; Base64 is fully reversible and grows with the input. Confusing the three β encoding, encryption, hashing β is one of the most common security misunderstandings in software.
Common mistakes
- Treating Base64 as security. βWe Base64-encoded the passwordβ is plaintext with extra steps.
- Forgetting the padding. Strict decoders reject unpadded input; lenient ones accept it. Know which one yours is before you strip
=. - Mixing standard and URL-safe alphabets.
+//vs-/_β pick the right decoder for the data you have. - Encoding text without fixing the character set first. Base64 encodes bytes, not characters: βcafΓ©β in UTF-8 and in Latin-1 give different output. Always encode UTF-8 explicitly.
- Base64-encoding large files into HTML. A 2 MB photo becomes 2.7 MB of text in your page, uncacheable and slow. Link the file instead.
Try it: Base64 Encoder
Encode your own text with the Base64 Encoder and decode it back β then try an image file and watch the output grow by a third, exactly as this guide predicts.
Keep learning
Frequently asked questions
Why does Base64 output sometimes end with = or ==?
Padding. Input is processed in 3-byte groups; if the last group has 2 bytes, one = is added, and if it has 1 byte, two ==. The padding keeps the output length a multiple of 4 and tells the decoder how many bytes the final group holds.
Is Base64 reversible?
Yes, completely β it is an encoding, not encryption or hashing. Anyone with the string can decode it with no key. Never use it to hide anything.
Why is Base64 output bigger than the input?
Each output character carries 6 bits but occupies 8 bits (one byte) of text. 3 bytes (24 bits) become 4 characters (32 bits) β a 33% overhead, plus up to 2 padding characters.
What is the difference between Base64 and hex encoding?
Both spell bytes as text. Hex uses 16 characters (0-9, a-f), so each byte becomes 2 characters β 100% overhead. Base64 uses 64 characters, so each 3 bytes become 4 characters β 33% overhead. Hex is more readable and easier to compare; Base64 is more compact.
Can Base64 encode Unicode text like Chinese or emoji?
Yes β but it encodes bytes, so the text must first be turned into bytes, almost always UTF-8. The same characters encoded as UTF-16 would give completely different Base64. If decoding gives mojibake, the byte encoding was wrong, not the Base64.