Base64 Encoding

Base64 Encoding

叠甲

限于知识水平,文章中可能存在错误,欢迎指正。

What is Base64?

Base64 is a binary-to-text encoding scheme. It represents binary data in a printable ASCII string format by translating it into a radix-64 representation.
Base64 encoding is commonly used when there is a need to transmit binary data over media that do not correctly handle binary data and is designed to deal with textual data belonging to the 7-bit US-ASCII charset only.
One example of such a system is Email (SMTP), which was traditionally designed to work with plain text data in the 7-bit US-ASCII character set. Although, It was later extended to support non US-ASCII text messages as well as non-text messages such as audio and images, It is still recommended to encode the data to ASCII charset for backward compatibility.
What is Base64 Encoding and How does it work?

  • Base64 用 6 个 bit 来表示一个字符,输入流中每 3 个 Bytes 由 4 个 Base64 字符表示
    1
    2
    3
    4
    3 Bytes = 3 * 8 bits = 24 bits = 4 * 6 bits
    Base64 Encoding算法会在46bit各自的头部添0
    4 * 6 bit -> 4 * 8 bits = 4 Bytes
    这即是Base64编码后数据长度增加1/3的原因
  • Base64 包含 64 个字符(26 个大写字母,26 个小写字母,10 个数字,2 个特殊字符+,/)
  • Base64 常用于在 URL、Cookie、网页中传输少量二进制数据。

How does Base64 Encoding work?

Base64 encoding works with a 65-character subset of the US-ASCII charset. The first 64 characters out of the 65-character subset are mapped to an equivalent 6-bit binary sequence $2^6 = 64$. The extra 65th character (=) is used for padding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
## The Base64 Alphabet

Value Encoding Value Encoding Value Encoding Value Encoding
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w (pad) =
15 P 32 g 49 x
16 Q 33 h 50 y

Steps

  1. 以 6 bit 为单位将二进制数据分组,不足 6 个 bit 的在末尾补 0,得到 8 组二进制数据:
    以 ASCII 编码的字符串HelloWorld\n为例,其二进制数据为:
    01001000 01100101 01101100 01101100 01101111 01010111 01101111 01110010 01101100 01100100 00001010
    分组后为:
    010010 000110 010101 101100 011011 000110 111101 010111 011011 110111 001001 101100 011001 000000 1010
  2. 将各组二进制转成十进制,根据映射关系得到 Base64 编码
    1
    2
    18 6  21 44 27 6  61 23 27 55 9  44 25 0  40
    S G V s b G 9 X b 3 J s Z A o
  3. 将 Base64 编码拼接起来,最终结果应为 4 的倍数,不足则在末尾补=
    SGVs bG9X b3Js ZAO=

Why Base64?

在早期的互联网协议中,设计者们主要考虑的是发送文本数据,因此这些协议通常只能处理 ASCII 字符集中的字符。然而,二进制文件(如图片、音频、视频等)包含的数据通常超出了 ASCII 字符集的范围。如果直接发送这些二进制数据,可能有部分字节被解释为控制字符,例如换行符、回车符、制表符等。这些控制字符在传输过程中可能会被解释为其他含义,从而导致数据传输错误。

Base64 编码出现是为了就是解决在网络上发送二进制数据的问题。Base64 选取 US-ASCII 中的64 个可打印字符作为编码后的字符集,这样就可以保证数据在传输过程中不会被解释为控制字符,从而确保数据传输的安全和可靠。

不过,Base64 编码会导致数据膨胀,大约增加 1/3 的大小。

Base64’s Applications

Base64 的应用非常广泛,包括但不限于:

  • 电子邮件:在 MIME(多用途互联网邮件扩展)协议中,Base64 用于编码电子邮件的附件,使其可以通过电子邮件系统发送。电子邮件系统主要设计用于处理文本,而不是二进制数据,因此需要将二进制附件(如图片或文档)转换为文本格式。

  • 数据 URI:在网页中,可以使用 Base64 编码的数据 URI 来内嵌图片或其他资源。这样可以减少 HTTP 请求的数量,从而提高页面加载速度。数据 URI 的格式为 data:[<mediatype>][;base64],<data>,其中 是资源的 MIME 类型, 是 Base64 编码的资源数据。

  • URL 编码:在某些情况下,URL 可能需要包含二进制数据。由于 URL 只能包含 ASCII 字符,因此需要使用 Base64 编码来编码这些数据。标准的 Base64 编码在 URL 中有特殊含义的字符(+ 和 /),于是 + 和 / 分别被替换为 - 和 _。

  • 证书和密钥:在 SSL/TLS 和其他安全协议中,证书和密钥通常使用 Base64 编码,以便在文本格式的配置文件中使用。这样可以避免二进制数据在处理或传输过程中可能出现的问题。

  • 编码用户凭据:在 HTTP 基本认证中,用户的用户名和密码会被拼接成一个字符串,然后使用 Base64 编码,最后将编码后的字符串放在 HTTP 头的 Authorization 字段中发送给服务器。

Author

Efterklang

Posted on

2024-01-24

Updated on

2024-09-18

Licensed under

Comments