← Back to Blog
Tech Basics2026-03-10 · 7 min read

What is Base64 Encoding? Principles, Use Cases & Online Converter

Base64 is an encoding scheme that converts binary data into ASCII text, widely used for email attachments, embedding images in HTML/CSS, and API data transfer. This guide covers everything from theory to practice.

# Base64# 编码# 前端开发# Web

What is Base64?

Base64 is an encoding scheme that represents binary data using 64 printable characters: uppercase A–Z (26), lowercase a–z (26), digits 0–9 (10), plus + and / (2) — totalling 64 characters, hence the name.

This encoding was originally designed for MIME (Multipurpose Internet Mail Extensions) to safely transmit binary data (images, files) over protocols that only support ASCII text.

How Base64 Works

The core logic is a 3 bytes → 4 characters conversion:

1. Group the binary data in chunks of 3 bytes (3 × 8 = 24 bits)

2. Split each 24-bit chunk into 4 groups of 6 bits

3. Each 6-bit value (0–63) maps to a character in the Base64 alphabet

Example: encoding Man:

  • M = 01001101, a = 01100001, n = 01101110
  • 24 bits in 6-bit groups: 010011 010110 000101 101110
  • Base64 chars: T, W, F, u → result: TWFu

Common Use Cases

1. Embedding Images in CSS/HTML (Data URI)

``css

.icon {

background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA...');

}

` Pros: reduces HTTP requests — great for small icons. Cons: encoded size is ~33% larger, so avoid for large images.

2. HTTP Basic Authentication

`

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

`
⚠️ Base64 is not encryption — it's trivially reversible. Always pair with HTTPS.

3. JWT (JSON Web Tokens)

JWTs use Base64URL (replacing + with - and / with _) for their three-part structure:

`

Header.Payload.Signature

`

4. Email Attachments

SMTP only transmits 7-bit ASCII. MIME + Base64 allows binary attachments to travel safely.

Online Base64 Tool

Need a quick encode/decode? Use our Image to Base64 tool — supports drag-and-drop for PNG, JPG, SVG, and more.

Base64 in JavaScript

`javascript

// Encode (ASCII only)

const encoded = btoa('Hello, World!'); // SGVsbG8sIFdvcmxkIQ==

// Decode

const decoded = atob('SGVsbG8sIFdvcmxkIQ=='); // Hello, World!

// For non-ASCII (e.g. Chinese characters):

const encodeChinese = btoa(unescape(encodeURIComponent('你好')));

``

Base64 vs Encryption

A very common misconception: Base64 is encoding, not encryption.

|---|---|---|

| Base64Encryption (e.g. AES)
ReversibleCompletely, no key neededRequires a key
PurposeFormat conversionData security
Size change+~33%Variable padding
Use MD5, SHA, or AES when you need actual security.

Summary

Base64's core value: convert arbitrary binary data to plain text so it can flow safely through text-only channels. Understanding when and why to use it is essential knowledge for every web developer.

W

woshiit 技术团队

woshiit AI Navigation · Original Content

More Articles

Tech Sharing

SBTI Personality Test: A 15-Dimension Character Analysis Beyond MBTI

12 min

AI Tools

NüWa.skill: Distilling the Minds of Legends — Deep Conversations with Jobs & Musk via AI

8 min

Security

What is MD5? A Deep Dive into the MD5 Hashing Algorithm and Its Security

8 min

Tech Basics

The Complete Guide to URL Encoding: Why Chinese Characters Become %E4%B8%AD

6 min