第二章

Base64 字符集

64 个字符的构成、索引对照表与填充机制

2.1 Composition of 64 Characters

Base64 gets its name from using 64 characters for encoding. These 64 characters were chosen because they are "safe" ASCII characters that are not easily misinterpreted or modified across various systems and protocols.

Index Range Characters Count Usage
0 - 25 A - Z 26 Uppercase
26 - 51 a - z 26 Lowercase
52 - 61 0 - 9 10 Numbers
62 + 1 Plus
63 / 1 Slash
(Padding) = - Special padding char, not in index

2.2 Base64 Index Table

This is the complete Base64 index table. In encoding and decoding, we map between Values (Index) and Characters using this table.

0 - 15
0: A
1: B
2: C
...
15: P
16 - 31
16: Q
...
25: Z
26: a
...
32 - 47
32: g
...
40: o
...
48 - 63
48: w
...
51: z
52: 0
62: +
63: /

Examples: Index 0 is A, Index 26 is a, Index 63 is /.

2.3 Padding Character =

Base64 encodes data in groups of 3 bytes (24 bits). If the input length is not a multiple of 3, the = character is used as padding.

Padding Rules:

  • Remainder 0 (Divisible) → No Padding
    Ex: "ABC" (3 bytes) → "QUJD"
  • Remainder 1 (1 byte left) → Add two '='
    Ex: "A" (1 byte) → "QQ=="
  • Remainder 2 (2 bytes left) → Add one '='
    Ex: "AB" (2 bytes) → "QUI="

Padding not only aligns data but also tells the decoder the exact length of original data.