Base64, Base64URL and Base32 all turn binary data into plain text, so it can travel through systems that were only built for text.
- Base64 is the most compact of the three.
- Base64URL is Base64 with two characters swapped, so the output is safe in URLs and filenames.
- Base32 trades size for a smaller, case-insensitive alphabet that is easier for humans to read and type.
Hey there! I ran into some encoding issues recently and realized I hadn’t seen a crisp, fun explanation of these three schemes anywhere. So I wrote one. Grab your gear, and let’s decode them together!
Base64 vs Base64URL vs Base32 at a glance
| Base64 | Base64URL | Base32 | |
|---|---|---|---|
| Alphabet | A–Z, a–z, 0–9, +, / | A–Z, a–z, 0–9, -, _ | A–Z, 2–7 |
| Bits per character | 6 | 6 | 5 |
| Input group → output | 3 bytes → 4 chars | 3 bytes → 4 chars | 5 bytes → 8 chars |
| Size growth | about 33% | about 33% | about 60% |
| Padding | = to a multiple of 4 | = optional, often dropped | = to a multiple of 8 |
| Case-sensitive | Yes | Yes | No |
| Typical uses | Email attachments, data URIs, APIs | JWTs, URL parameters, filenames | TOTP secret keys, Tor onion addresses, codes people type by hand |
The Enigma of Base64 Encoding
Why do we need Base64?
Imagine you’re sending a beautiful picture postcard through the digital world. The catch is that the postal service (the internet, in this case) only handles plain text. How do you do it?
Enter Base64. It transforms binary data, like images, into text that can travel through text-only channels without getting corrupted.
Base64 represents your data using 64 characters, in this order:
- 26 uppercase letters, A–Z (values 0–25)
- 26 lowercase letters, a–z (values 26–51)
- 10 digits, 0–9 (values 52–61)
- Two special characters,
+(62) and/(63)
How Base64 works, step by step
- Grouping bytes. The input bytes are grouped into sets of three, giving 24 bits per group.
- Dividing bits. Those 24 bits are split into four chunks of 6 bits each.
- Mapping to characters. Each 6-bit chunk holds a value from 0 to 63. That value maps to exactly one character in the Base64 alphabet.
- Padding. If the input length isn’t divisible by three,
=characters are added so the output length is a multiple of four. This lets the decoder split the text back into the original bytes cleanly.
A simple text like Hello! (6 bytes, exactly two groups of three) encodes to SGVsbG8h. It needs no padding at all.
Because every 3 bytes become 4 characters, Base64 output is about a third larger than the input. You’ll find it in email attachments, data URIs on web pages, and anywhere binary data has to squeeze into a text-only zone.
Usage of Base64 in Data URIs
Data URIs let you embed binary data, such as images, directly in HTML or CSS using Base64. No external file reference is needed, which means one less HTTP request.
Embedding an image in HTML using a data URI
Say you have a small logo or icon that you want to include directly in your page:
- Original image: an image file,
logo.png. - Base64 encoding: convert
logo.pnginto a Base64 string. It will be long. - Embed in HTML: put the string in the
<img>tag’ssrcattribute:
<img src="data:image/png;base64,Base64EncodedStringHere" alt="Logo">Replace Base64EncodedStringHere with the actual encoded string. The data:image/png;base64, prefix tells the browser that a Base64-encoded PNG follows.
Keep it small. The encoded image is about a third bigger than the file. It also can’t be cached separately from the page that contains it. Data URIs work best for tiny icons and similar small assets, not large images.
Navigating the Waters of Base64URL
But, oh, the plot thickens with Base64URL, a close cousin of Base64 tailored for the web.
The twist is that standard Base64 uses + and /, and both mean something special in URLs and file paths. Base64URL swaps them for safe characters:
+becomes-/becomes_
Here are the two bytes 0xFB 0xFF in each scheme:
| Scheme | Output |
|---|---|
| Base64 | +/8= |
| Base64URL | -_8=, or -_8 with the padding dropped |
The padding = is often dropped too, because = also has a meaning in query strings. JSON Web Tokens (JWTs) are a well-known example: each part of a JWT is Base64URL-encoded without padding.
If a strict decoder complains about such a string, add back = until the length is a multiple of four, then decode.
The Expedition to Base32
Then there’s Base32, another stop on our adventure. It uses only 32 characters, the uppercase letters A–Z and the digits 2–7, so each character carries 5 bits.
How Base32 works
- Input bytes are grouped into sets of five, giving 40 bits.
- Those 40 bits are split into eight 5-bit chunks.
- Each chunk (value 0–31) maps to one character.
=padding brings the output to a multiple of eight characters.
Hello! in Base32 becomes JBSWY3DPEE======. Compare that with SGVsbG8h in Base64: longer, but easier to read aloud.
Why choose Base32 over Base64?
- Case-insensitive.
jbswy3dpandJBSWY3DPdecode to the same bytes. - Fewer look-alikes. It skips the digits 0, 1, 8 and 9, which are easily confused with O, I and B.
- Safe almost everywhere. It contains no
+,/or lowercase letters that might get mangled in URLs, filenames or case-insensitive systems.
The price is size. Base32 output is about 60% larger than the input, compared with about 33% for Base64.
You’ve probably met Base32 already. The secret key you type into an authenticator app for one-time codes (TOTP) is usually Base32, and so are Tor onion addresses. Both are places where humans read, copy or type the string.
Try It Yourself
On Linux or macOS with GNU coreutils:
echo -n 'Hello!' | base64 # SGVsbG8hecho -n 'Hello!' | base32 # JBSWY3DPEE======echo -n 'SGVsbG8h' | base64 -d # Hello!In Python, everything lives in the standard base64 module:
import base64
data = b"Hello!"base64.b64encode(data) # b'SGVsbG8h'base64.urlsafe_b64encode(b"\xfb\xff") # b'-_8='base64.b32encode(data) # b'JBSWY3DPEE======'base64.b32decode(b"JBSWY3DPEE======") # b'Hello!'Why These Encoding Schemes Matter
Why do we bother with all these encoding shenanigans? It’s all about compatibility and safety in transit. These schemes let binary data cross channels that only support text, so it arrives intact and unaltered at its destination.
Encoding is not encryption
One warning from the security side of the map: Base64, Base64URL and Base32 hide nothing. Anyone can decode them instantly, with no key.
If you see a password or token “protected” only by Base64, treat it as plain text. HTTP Basic authentication is a good example: it Base64-encodes the username and password, which is why it must always run over HTTPS.
Choosing Your Path
- Use Base64 when you need a compact text form of binary data. It fits email attachments, data URIs, and APIs that expect the format.
- Opt for Base64URL when the data goes into URLs, query strings, filenames or tokens like JWTs.
- Choose Base32 when people will read, type or copy the string by hand, or when the system around it is case-insensitive.
Alternatives and Mysteries Beyond
Our adventure doesn’t end here:
- Base58 was popularized by Bitcoin. It drops look-alike characters such as 0, O, I and l, while staying more compact than Base32.
- Hexadecimal (Base16) is the simplest form. Each byte becomes two characters from 0–9 and a–f, so
Hello!is48656c6c6f21. It doubles the size but is very easy to read while programming and debugging.
FAQ
What’s the difference between Base32 and Base64? Base64 uses 64 characters (6 bits each) and makes output about 33% larger than the input. Base32 uses 32 characters (5 bits each), A–Z and 2–7, and makes output about 60% larger. In return, Base32 is case-insensitive and easier for people to read and type.
Why does Base64 output end with = or ==?
That’s padding. When the input length isn’t a multiple of three bytes, = fills the last group so the output length is a multiple of four.
Is Base64URL compatible with Base64?
Almost. The only differences are - and _ in place of + and /, plus padding that is often omitted. Swap those two characters back and restore the padding, and a standard Base64 decoder will read it.
Is Base64 a form of encryption? No. It’s a reversible encoding with no key, so anyone can decode it. Never use it to protect secrets.
When should I avoid data URIs? Avoid them for anything larger than small icons. The encoded data is bigger than the file and can’t be cached separately.
So, choose wisely! Whether you’re encoding treasure maps for your fellow digital pirates or just making sure your data travels safely, the right encoding at the right time is the difference between smooth sailing and getting lost at sea.
Until our next digital odyssey, keep exploring and encoding. Thanks for reading!
[keep: comments section]