What is Base64 Encoding?

Base64 is a binary-to-text encoding scheme that converts binary data into a sequence of 64 printable ASCII characters. It's one of the most widely used encoding methods in computing, essential for transmitting data over text-based protocols.

Quick Summary

  • Converts binary data to 64 ASCII characters (A-Z, a-z, 0-9, +, /)
  • Output is approximately 33% larger than input
  • Defined in RFC 4648
  • Not encryption—anyone can decode it

What is Base64?

Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format. The name comes from the fact that it uses 64 different characters to represent data: the letters A-Z (26), a-z (26), digits 0-9 (10), and two additional characters (+ and /).

The encoding was originally designed for use in MIME (Multipurpose Internet Mail Extensions) to safely transmit binary attachments through email systems that only handled ASCII text.

How Base64 Encoding Works

Base64 works by taking groups of 3 bytes (24 bits) of binary data and splitting them into 4 groups of 6 bits each. Each 6-bit group (which can represent values 0-63) is then mapped to one of the 64 characters in the Base64 alphabet.

The Base64 Alphabet

Index 0-25: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Index 26-51: a b c d e f g h i j k l m n o p q r s t u v w x y z
Index 52-61: 0 1 2 3 4 5 6 7 8 9
Index 62-63: + /
Padding: = (used when input isn't divisible by 3)

Step-by-Step Example

Let's encode the word "Man" (a classic example from the Wikipedia Base64 article):

StepData
1. ASCII valuesM=77, a=97, n=110
2. Convert to binary01001101 01100001 01101110
3. Regroup into 6-bit chunks010011 010110 000101 101110
4. Convert to decimal19, 22, 5, 46
5. Map to Base64 charsT W F u

Result: Man → TWFu

Padding with =

When the input length isn't divisible by 3, Base64 adds = padding characters to make the output a multiple of 4 characters:

  • Ma (2 bytes) → TWE= (one = padding)
  • M (1 byte) → TQ== (two = padding)
  • Man (3 bytes) → TWFu (no padding needed)

Why Use Base64?

Base64 solves a critical problem: safely transmitting binary data through text-only channels.

1. Email Attachments

The original email protocol (SMTP) was designed for 7-bit ASCII text only. Binary files like images or PDFs would be corrupted during transmission. Base64 encoding (via MIME) ensures attachments survive intact.

2. Embedding Data in Text Formats

JSON, XML, HTML, and CSS are text-based formats. To include binary data (like an image), you must convert it to text first. Base64 enables Data URIs for embedding images directly in web pages.

3. HTTP Headers and URLs

HTTP headers must be ASCII. The Authorization header for HTTP Basic Auth uses Base64 to encode credentials. For URLs, the Base64URL variant avoids problematic characters.

Common Use Cases

📧 Email Attachments

MIME encoding for sending binary files through SMTP.

🖼️ Data URIs

Embed images directly in HTML/CSS. Learn how Data URIs use Base64 →

🔐 HTTP Basic Auth

Encode username:password in Authorization headers.

🎫 JWT Tokens

JSON Web Tokens use Base64URL. Learn how JWTs use Base64URL →

📦 API Payloads

Transmit binary data in JSON API requests/responses.

🔑 Cryptographic Keys

Store keys and certificates in PEM format.

Size Overhead

Base64 encoding increases data size by approximately 33%. This is because every 3 bytes of input becomes 4 bytes of output (a 4:3 ratio).

Original size: 3 bytes (24 bits)
Encoded size: 4 characters (32 bits)
Overhead: 4/3 = ~1.33x or 33% increase

For a 1 MB image, the Base64 encoded version will be approximately 1.33 MB. Consider this overhead when deciding whether to use Base64 for large files—sometimes a direct binary transfer is more efficient.

Base64 is NOT Encryption

⚠️Security Warning
Base64 is an encoding, not encryption. Anyone can decode Base64 data instantly—it provides zero security. Never use Base64 alone to "hide" sensitive information like passwords, API keys, or personal data.

If you need to protect data, use proper encryption (like AES or RSA) before optionally Base64 encoding the encrypted output for transport.

RFC Standards

Base64 encoding is defined in several RFC (Request for Comments) documents:

Quick Code Examples

Here's how to encode and decode Base64 in popular languages. See our complete programming language guide for more examples.

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

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

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

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

Try It Yourself

Use our free Base64 encoder/decoder to convert data instantly. 100% client-side—your data never leaves your browser.

Open Encoder/Decoder →