Back to Blog

Base64 Encoding Explained: When and Why to Use It

December 2, 2025·9 min read
encodingbase64basics

What is Base64 encoding, why does it exist, and when should you use it? A practical guide.

You've seen Base64 strings before, even if you didn't know what they were. That long string of random-looking characters in a data URL? Base64. The garbled text when you view an email's raw source? Base64. That weirdly long string in a JSON API response? Probably Base64.

But what actually is Base64, and why do we need it? Let me demystify this encoding scheme that's been around since the early days of email.

The Problem Base64 Solves

To understand Base64, you first need to understand the problem it was created to solve.

Back in the early days of the internet, systems were designed primarily for text. Email protocols, for example, were built to handle ASCII characters: the 128 standard characters that include letters, numbers, and basic punctuation.

But what happens when you want to send a photo in an email? Or transmit binary data through a text-based protocol? Binary data can contain any byte value from 0 to 255, and many of those values don't map to printable characters. Some might even be interpreted as control characters that could break the transmission.

Base64 solves this by converting binary data into a string that uses only 64 "safe" characters:

A-Z (26 characters)
a-z (26 characters)
0-9 (10 characters)
+ and / (2 characters)

These characters are guaranteed to survive any text-based transmission without being mangled or misinterpreted.

How Base64 Actually Works

Let's peek under the hood. Base64 encoding works by taking binary data and converting it in groups of 3 bytes (24 bits) into 4 characters.

Here's a simplified example with the word "Hi":

  1. Start with ASCII values: H=72, i=105
  2. Convert to binary: 01001000 01101001
  3. Regroup into 6-bit chunks: 010010 000110 1001
  4. Pad to make complete groups: 010010 000110 100100
  5. Convert each 6-bit value to a Base64 character

The result? "Hi" becomes "SGk="

The = at the end is padding: it indicates that the original data wasn't perfectly divisible by 3 bytes.

Try it yourself: paste "Hi" into our Base64 Encoder/Decoder and you'll get "SGk=". Paste "SGk=" in the decode field and you'll get "Hi" back.

Common Use Cases

Embedding Images in HTML/CSS

One of the most common uses of Base64 is embedding small images directly in HTML or CSS:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" />

This is a data URL: the image data is embedded directly in the src attribute. The browser decodes the Base64 and renders the image without needing a separate HTTP request.

When to use this:

  • Small images (icons, simple graphics)
  • Images that need to be self-contained (emails, single-file exports)
  • Reducing HTTP requests for tiny assets

When NOT to use this:

  • Large images (the Base64 string is ~33% larger than the original file)
  • Images that need to be cached separately
  • When file size is critical

Our Image ↔ Base64 tool makes this conversion trivial. Upload an image, get the data URL. Paste a data URL, see the image.

Sending Binary Data in JSON

JSON only supports strings, numbers, booleans, arrays, and objects. There's no native way to include binary data like files or images.

The solution? Encode the binary data as a Base64 string:

{
  "filename": "report.pdf",
  "content": "JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDY2Pj5zdHJlYW0KeJwr5HIK4TI2..."
}

The receiving system decodes the Base64 string back to binary and saves it as a file.

HTTP Basic Authentication

Ever wondered how HTTP Basic Auth works? When you provide a username and password, they're combined and Base64 encoded:

username:password

Becomes:

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

You can decode dXNlcm5hbWU6cGFzc3dvcmQ= with our Base64 Encoder/Decoder to verify this yourself. Go ahead, try it: you'll get "username:password".

Important security note: This is encoding, not encryption. Anyone who intercepts this header can decode it instantly. This is why Basic Auth should only be used over HTTPS.

Email Attachments

When you attach a file to an email, the email client encodes it as Base64. This is because email protocols (SMTP) were designed for 7-bit ASCII text.

If you've ever looked at the raw source of an email, you've seen this:

Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="document.pdf"

JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9GaWx0ZXIvRmxhdGVEZWNvZGUv
TGVuZ3RoIDY2Pj5zdHJlYW0KeJwr5HIK4TI2UjBUMAQxuFxDuJz1XEM4HYO4
...

That long string of characters IS your PDF, encoded as Base64.

Obfuscation (But Not Security!)

Sometimes you'll see Base64 used to "hide" data: maybe a config file has passwords encoded as Base64, or a script has encoded strings to make them less obvious.

This is obfuscation, not security. Anyone can decode Base64 in seconds. It might prevent casual snooping, but it provides zero protection against anyone who actually tries.

If you need to protect sensitive data, use proper encryption. Base64 is for encoding, not securing.

The 33% Size Penalty

There's a catch with Base64: encoded data is always larger than the original.

Specifically, Base64 encoding increases the size by approximately 33%. A 100KB image becomes about 133KB when Base64 encoded.

This happens because we're converting from 8-bit bytes to 6-bit characters. Three bytes (24 bits) become four characters. 4/3 ≈ 1.33.

For small data, this overhead is negligible. For large files, it adds up quickly. A 10MB file becomes 13.3MB. If you're dealing with large files, it's usually better to upload them separately rather than embedding them as Base64.

Base64 Variants

There are actually several variants of Base64, each designed for specific use cases:

Standard Base64: Uses + and / as the 63rd and 64th characters. This is what most systems use.

URL-safe Base64: Uses - and _ instead of + and /. This is important because + and / have special meanings in URLs. If you need to put Base64 data in a URL parameter, use this variant.

Base64 without padding: Some systems omit the = padding characters. The decoder can figure out the padding from the length of the string.

Most tools handle these variants automatically, but it's good to be aware they exist if you run into decoding issues.

Decoding Unknown Strings

Sometimes you encounter a string and think "that looks like Base64." Here's how to tell:

  1. Length is a multiple of 4 (or would be with 1-2 = padding characters)
  2. Only contains A-Za-z0-9+/= (or -_= for URL-safe variant)
  3. Ends with 0-2 = characters (padding)

If it matches these criteria, try decoding it. Our Base64 Encoder/Decoder will show you instantly whether it's valid Base64 and what it contains.

Fair warning: you might find some interesting things. I once decoded a seemingly random string in a config file and found my own email address. Always fun.

Quick Reference

When to use Base64:

  • Embedding small images in HTML/CSS
  • Including binary data in JSON
  • Transmitting data through text-only channels
  • Email attachments (handled automatically)

When NOT to use Base64:

  • Large files (size increases 33%)
  • Security (it's encoding, not encryption)
  • Storage (just store the binary data directly)
  • Performance-critical applications

Tools:

Base64 isn't glamorous, but it's everywhere. Now you know how it works and when to use it.

Tools mentioned in this article:

Related Tools

More Articles