什么是 Base64?

Base64 是一种基于 64 个可打印字符来表示二进制数据的编码方法。它将二进制数据转换为由 A-Z、a-z、0-9、+、/ 共 64 个字符组成的 ASCII 字符串,常用于在文本协议中传输二进制数据。

Base64 编码后的数据比原始数据大约增加 33%,因为每 3 个字节的二进制数据会被编码为 4 个 Base64 字符。

Base64 编码原理

1

分组

将输入数据按 3 个字节(24 位)为一组进行分组

2

重新划分

将 24 位数据重新划分为 4 个 6 位的组

3

查表

每个 6 位值(0-63)对应 Base64 字符表中的一个字符

4

填充

如果最后一组不足 3 字节,用 = 进行填充

Base64 字符表

值 0-25A-Z(大写字母)
值 26-51a-z(小写字母)
值 52-610-9(数字)
值 62+(加号)
值 63/(斜杠)
填充=(等号)

Base64 编码示例

原始文本
Hello
ASCII 码
72 101 108 108 111
二进制
01001000 01100101 01101100 01101100 01101111
Base64 编码
SGVsbG8=

编程语言中使用 Base64

JavaScript / Node.js

// 编码
const encoded = btoa('Hello World');
console.log(encoded); // "SGVsbG8gV29ybGQ="

// 解码
const decoded = atob('SGVsbG8gV29ybGQ=');
console.log(decoded); // "Hello World"

// Node.js 环境
const encoded = Buffer.from('Hello World').toString('base64');
const decoded = Buffer.from('SGVsbG8gV29ybGQ=', 'base64').toString('utf8');

Python

import base64

# 编码
encoded = base64.b64encode('Hello World'.encode()).decode()
print(encoded)  # "SGVsbG8gV29ybGQ="

# 解码
decoded = base64.b64decode('SGVsbG8gV29ybGQ=').decode()
print(decoded)  # "Hello World"

Java

import java.util.Base64;

// 编码
String encoded = Base64.getEncoder()
    .encodeToString("Hello World".getBytes());
System.out.println(encoded); // "SGVsbG8gV29ybGQ="

// 解码
byte[] decoded = Base64.getDecoder()
    .decode("SGVsbG8gV29ybGQ=");
String text = new String(decoded);
System.out.println(text); // "Hello World"

PHP

// 编码
$encoded = base64_encode('Hello World');
echo $encoded; // "SGVsbG8gV29ybGQ="

// 解码
$decoded = base64_decode('SGVsbG8gV29ybGQ=');
echo $decoded; // "Hello World"

Go

import "encoding/base64"

// 编码
encoded := base64.StdEncoding.EncodeToString([]byte("Hello World"))
fmt.Println(encoded) // "SGVsbG8gV29ybGQ="

// 解码
decoded, _ := base64.StdEncoding.DecodeString("SGVsbG8gV29ybGQ=")
fmt.Println(string(decoded)) // "Hello World"

C#

using System;

// 编码
string encoded = Convert.ToBase64String(
    System.Text.Encoding.UTF8.GetBytes("Hello World"));
Console.WriteLine(encoded); // "SGVsbG8gV29ybGQ="

// 解码
string decoded = System.Text.Encoding.UTF8.GetString(
    Convert.FromBase64String("SGVsbG8gV29ybGQ="));
Console.WriteLine(decoded); // "Hello World"

Ruby

require 'base64'

# 编码
encoded = Base64.encode64('Hello World')
puts encoded # "SGVsbG8gV29ybGQ=\n"

# 解码
decoded = Base64.decode64('SGVsbG8gV29ybGQ=')
puts decoded # "Hello World"

Shell / 命令行

# 编码
echo -n "Hello World" | base64
# 输出: SGVsbG8gV29ybGQ=

# 解码
echo "SGVsbG8gV29ybGQ=" | base64 -d
# 输出: Hello World

Base64 常见应用场景

📧 邮件传输

在 MIME 协议中传输二进制附件,如图片、文档等

🖼️ 图片嵌入

将小图片编码为 Base64 直接嵌入 HTML 或 CSS 中

🔐 认证信息

HTTP Basic Authentication 中编码用户名和密码

📄 数据存储

在 JSON 或 XML 等文本格式中存储二进制数据

🔑 JWT 令牌

JSON Web Token 使用 Base64 编码 header 和 payload

📋 数据 URL

Data URI scheme 中嵌入小型资源文件

常见问题

Q: Base64 是加密吗?

A: 不是。Base64 是编码方式,不是加密算法。任何人都可以解码 Base64 字符串,它不提供任何安全性。不要用 Base64 来保护敏感数据。

Q: Base64 编码后数据为什么会变长?

A: Base64 将每 3 个字节编码为 4 个字符,因此编码后的数据长度约为原始数据的 4/3(约 133%)。这是 Base64 算法的固有特性。

Q: Base64 和 Base64URL 有什么区别?

A: Base64URL 是 Base64 的变体,专门用于 URL 安全场景。它将 + 替换为 -,将 / 替换为 _,并通常省略末尾的 = 填充。

Q: 为什么 Base64 编码结果末尾有 = 号?

A: = 是填充字符。当输入数据长度不是 3 的倍数时,需要用 = 填充到 4 的倍数长度。一个 = 表示缺少 1 字节,两个 = 表示缺少 2 字节。