GeekCoding101 make your way to geek

Base32 vs Base64 vs Base64URL: How Each Encoding Works

Base32 vs Base64 vs Base64URL in plain English: alphabets, padding, size overhead, worked examples, data URIs, and when to choose each one.

7 min read
A Guide To Base64 Base64url And Base32 Encoding
On this pageBase64 vs Base64URL vs Base32 at a glance

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

Base64Base64URLBase32
AlphabetA–Z, a–z, 0–9, +, /A–Z, a–z, 0–9, -, _A–Z, 2–7
Bits per character665
Input group → output3 bytes → 4 chars3 bytes → 4 chars5 bytes → 8 chars
Size growthabout 33%about 33%about 60%
Padding= to a multiple of 4= optional, often dropped= to a multiple of 8
Case-sensitiveYesYesNo
Typical usesEmail attachments, data URIs, APIsJWTs, URL parameters, filenamesTOTP 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:

  1. 26 uppercase letters, A–Z (values 0–25)
  2. 26 lowercase letters, a–z (values 26–51)
  3. 10 digits, 0–9 (values 52–61)
  4. Two special characters, + (62) and / (63)

How Base64 works, step by step

  1. Grouping bytes. The input bytes are grouped into sets of three, giving 24 bits per group.
  2. Dividing bits. Those 24 bits are split into four chunks of 6 bits each.
  3. 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.
  4. 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:

  1. Original image: an image file, logo.png.
  2. Base64 encoding: convert logo.png into a Base64 string. It will be long.
  3. Embed in HTML: put the string in the <img> tag’s src attribute:
<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.

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:

SchemeOutput
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

  1. Input bytes are grouped into sets of five, giving 40 bits.
  2. Those 40 bits are split into eight 5-bit chunks.
  3. Each chunk (value 0–31) maps to one character.
  4. = 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. jbswy3dp and JBSWY3DP decode 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:

Terminal window
echo -n 'Hello!' | base64 # SGVsbG8h
echo -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! is 48656c6c6f21. 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]

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.

Keep reading

All posts