正在加载,请稍候…

Base64 Encoding Explained: What It Is and When to Use It

Learn what Base64 encoding is, how the algorithm works, common use cases in web development (data URIs, JWT), and how to encode/decode strings and files online.

What Is Base64 Encoding?

Base64 is an encoding scheme that converts binary data to a text format using a 64-character alphabet (A-Z, a-z, 0-9, +, /). It's not encryption — it provides no security — but it's the universal solution for transmitting binary data through text-only channels.

Why Base64 Exists

The original internet protocols (SMTP, HTTP/1.0, JSON) were designed for 7-bit ASCII text. Binary data — images, files, cryptographic keys — cannot be safely transmitted through these channels because:

  • Certain byte values have special meaning (null bytes, newlines, control characters)
  • Some systems strip or transform high-bit characters
  • Binary data may contain sequences that resemble protocol commands

Base64 solves this by converting any binary data to a safe subset of printable ASCII characters.

The Base64 Character Set

Standard Base64 uses 64 printable characters:

  • A-Z (26 characters)
  • a-z (26 characters)
  • 0-9 (10 characters)
    • and / (2 characters)
  • = for padding

This 64-character set can be represented with 6 bits per character (2^6 = 64).

How Base64 Encoding Works

Base64 converts 3 bytes (24 bits) to 4 characters (4 × 6 bits = 24 bits):

  1. Take 3 bytes: 01001101 01100001 01101110 ("Man")
  2. Group into 6-bit chunks: 010011 010110 000101 101110
  3. Map to Base64 characters: T W F u
  4. Result: "TWFu"

When the input isn't divisible by 3, = padding is added:

  • 1 extra byte → 2 characters + "=="
  • 2 extra bytes → 3 characters + "="

Size Overhead

Base64 increases data size by approximately 33%:

  • 3 bytes in → 4 bytes out (ratio = 1.333...)
  • 1 MB file becomes ~1.37 MB as Base64

With line breaks (MIME requires 76-char lines), overhead increases to ~37%.

Base64 Variants

Standard Base64 (RFC 4648)

Uses + and /. Padding with =. Used in MIME email, data URIs.

URL-Safe Base64 (RFC 4648 §5)

Replaces + with - and / with _ to avoid URL encoding conflicts. Used in JWT tokens, URL parameters, filenames.

Base64 Without Padding

Some systems (like many JWT implementations) omit the = padding. The decoder must handle this.

MIME Base64

Standard Base64 with 76-character line breaks ( ). Used in email attachments.

Common Uses of Base64

Data URLs (CSS/HTML)

Embed small images directly in CSS or HTML without separate files:

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

JWT Tokens

All three parts of a JWT (header, payload, signature) are Base64URL encoded.

API Authentication

HTTP Basic Auth encodes credentials as Base64:

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

Cryptographic Keys

PEM format (used for SSL certificates, SSH keys) wraps Base64-encoded DER binary:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...
-----END PUBLIC KEY-----

Email Attachments (MIME)

All email attachments are Base64 encoded in the MIME multipart format.

Decoding in JavaScript and Python

// Encode
const encoded = btoa('Hello, World!');  // 'SGVsbG8sIFdvcmxkIQ=='
// Decode
const decoded = atob('SGVsbG8sIFdvcmxkIQ==');  // 'Hello, World!'
// For binary data, use Uint8Array approaches
import base64
encoded = base64.b64encode(b'Hello, World!').decode()  # 'SGVsbG8sIFdvcmxkIQ=='
decoded = base64.b64decode('SGVsbG8sIFdvcmxkIQ==')  # b'Hello, World!'

Using the Base64 String Converter

Our tool:

  1. Encode text or data — convert any string to Base64
  2. Decode Base64 — restore original content from encoded string
  3. Choose variant — standard, URL-safe, or MIME (with line breaks)
  4. Unicode support — properly handles non-ASCII text with UTF-8
  5. Copy result — one-click copy of encoded/decoded output

Use it for generating data URLs, debugging JWT contents, encoding API credentials, and understanding Base64-encoded data in web applications.