正在加载,请稍候…

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

Learn what Base64 encoding is, how to encode and decode strings and files, when (and when not) to use it, and how it compares to hex and binary encoding.

What Is Base64 Encoding?

Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters. The name "Base64" comes from the radix-64 representation. The 64 characters used are: A-Z (26), a-z (26), 0-9 (10), + and / (2), with = used as padding.

Base64 was designed to safely transmit binary data over channels designed to handle text, such as email (SMTP) or embedding binary content in XML or JSON.

How Base64 Encoding Works

Base64 takes 3 bytes (24 bits) of input at a time and produces 4 characters of output. Each output character represents 6 bits of input data.

Input bytes:    01001101  01100001  01101110
Group into 6:   010011  010110  000101  101110
Base64 index:   19      22      5       46
Base64 chars:   T       W       F       u

The encoding increases size by approximately 33% (every 3 bytes becomes 4 characters). If the input isn't divisible by 3, = padding is added.

Base64 Encoding Examples

Input:    "Hello"
Base64:   SGVsbG8=

Input:    "Man"
Base64:   TWFu

Input:    "Ma"  (needs padding)
Base64:   TWE=

Common Uses of Base64

HTTP Basic Authentication

HTTP Basic Auth encodes credentials as username:password in Base64:

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

This encoding is trivially reversible and provides no security without HTTPS.

Data URIs

Embed images directly in HTML without separate HTTP requests:

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

JWT Tokens

JSON Web Tokens encode their header and payload as Base64URL (a URL-safe variant of Base64).

Email Attachments (MIME)

MIME encoding uses Base64 to attach binary files (images, PDFs, executables) to email messages.

SSH Public Keys

SSH public keys are stored and transmitted in Base64 format.

Base64 vs Base64URL

Standard Base64 uses + and / which are special characters in URLs. Base64URL replaces these:

  • + becomes -
  • / becomes _
  • = padding is removed

Use Base64URL whenever the encoded string will appear in URLs, filenames, or HTTP headers.

Base64 Is NOT Encryption

This is the most common misconception. Base64 is trivially reversible without any key:

atob('SGVsbG8=') // "Hello"

Anyone who sees Base64-encoded data can immediately decode it. Never use Base64 to hide passwords, API keys, or sensitive information.

Size Overhead

Base64 encoding increases data size by approximately 33%:

  • 100 bytes becomes approximately 136 Base64 characters
  • 1 MB image becomes approximately 1.37 MB Base64 string

Decoding Base64 in Code

// Browser
const decoded = atob('SGVsbG8=');  // "Hello"
const encoded = btoa('Hello');     // "SGVsbG8="

// Node.js
Buffer.from('SGVsbG8=', 'base64').toString('utf8')  // "Hello"
Buffer.from('Hello').toString('base64')              // "SGVsbG8="
import base64
base64.b64decode('SGVsbG8=').decode()   # "Hello"
base64.b64encode(b'Hello').decode()     # "SGVsbG8="

When to Use Base64

Use Base64 when embedding binary data in text formats (HTML, CSS, JSON, XML), transmitting binary over text protocols, or encoding binary data that must survive text transformations.

Do not use Base64 when you need security (it provides none), when size efficiency matters (it adds 33% overhead), or when the data is already text.

-> Try the Base64 String Converter