Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It's widely used in web development for embedding images, transmitting data through APIs, and handling file uploads.

What is Base64?

Base64 encoding converts binary data into a set of 64 printable ASCII characters:

  • Uppercase letters: A-Z (26 characters)
  • Lowercase letters: a-z (26 characters)
  • Numbers: 0-9 (10 characters)
  • Special characters: + and / (2 characters)
  • Padding character: = (used when input isn't divisible by 3)

This makes it safe to transmit binary data through systems designed for text, such as email or JSON APIs.

How Base64 Works

The encoding process:

  1. Take 3 bytes (24 bits) of binary data
  2. Split into 4 groups of 6 bits each
  3. Convert each 6-bit group to its Base64 character
  4. If input isn't divisible by 3, add padding (=)

Example

Encoding "Hi":

ASCII:    H         i
Binary:   01001000  01101001

Split into 6-bit groups:
010010  000110  1001xx

With padding (xx = 00):
010010  000110  100100

Base64 index: 18  6  36
Characters:   S   G   k  =

Result: "SGk="

Size Increase

Base64 encoding increases data size by approximately 33%. Three bytes become four characters.

Common Use Cases

Data URIs (Inline Images)

Embed images directly in HTML or CSS:

<img src="data:image/png;base64,iVBORw0KGgo...">

/* In CSS */
.icon {
    background-image: url(data:image/svg+xml;base64,PHN2Zw...);
}

Email Attachments (MIME)

Email protocols can only handle text, so attachments are Base64 encoded:

Content-Type: application/pdf
Content-Transfer-Encoding: base64

JVBERi0xLjQKJeLjz9MKMSAwIG9...

API Data Transmission

Send binary data in JSON payloads:

{
    "filename": "document.pdf",
    "content": "JVBERi0xLjQKJeLjz9MKMSAwIG9...",
    "encoding": "base64"
}

Basic Authentication

HTTP Basic Auth encodes credentials in Base64:

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

// Decoded: username:password

Security Warning

Base64 is NOT encryption! It's easily reversible. Never use it to "hide" sensitive data - always use proper encryption.

Implementation Examples

JavaScript

// Encode
const encoded = btoa('Hello, World!');
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="

// Decode
const decoded = atob('SGVsbG8sIFdvcmxkIQ==');
console.log(decoded); // "Hello, World!"

// For Unicode strings
const encodeUnicode = (str) => btoa(encodeURIComponent(str));
const decodeUnicode = (str) => decodeURIComponent(atob(str));

PHP

// Encode
$encoded = base64_encode('Hello, World!');
echo $encoded; // "SGVsbG8sIFdvcmxkIQ=="

// Decode
$decoded = base64_decode('SGVsbG8sIFdvcmxkIQ==');
echo $decoded; // "Hello, World!"

// Encode file
$fileContent = file_get_contents('image.png');
$base64Image = base64_encode($fileContent);

Python

import base64

# Encode
encoded = base64.b64encode(b'Hello, World!').decode('utf-8')
print(encoded)  # "SGVsbG8sIFdvcmxkIQ=="

# Decode
decoded = base64.b64decode('SGVsbG8sIFdvcmxkIQ==').decode('utf-8')
print(decoded)  # "Hello, World!"

When NOT to Use Base64

Large Files

The 33% size increase means:

  • 1 MB file becomes ~1.33 MB
  • Increased bandwidth usage
  • Slower page loads for inline images

Alternative: Serve files directly via HTTP with proper caching.

Security/Encryption

Base64 is easily decoded. For sensitive data:

  • Use AES or other encryption algorithms
  • Use HTTPS for transmission
  • Store passwords with bcrypt/Argon2

Storage in Databases

Storing Base64 in text columns wastes space:

  • Use BLOB/BYTEA columns for binary data
  • Or store files on disk/object storage

Base64 Variants

Standard Base64 (RFC 4648)

Uses A-Z, a-z, 0-9, +, / and = for padding.

URL-Safe Base64

Replaces + with - and / with _ to be URL-friendly:

Standard: "a+b/c=="
URL-safe: "a-b_c"

Base64url (RFC 4648 Section 5)

URL-safe version often used in JWTs and OAuth tokens.

Conclusion

Base64 is a simple but powerful encoding that bridges the gap between binary and text-based systems. Use it wisely for:

  • Small inline images and icons
  • API data transmission
  • Email attachments

Avoid it for large files, security purposes, or when binary storage is available.

Try our Base64 Encoder/Decoder to quickly encode or decode your data.