输入

输出

About Base64

Base64 is a binary-to-text encoding scheme that represents binary data in ASCII format. It's commonly used when there's a need to encode binary data for storage or transfer in environments that only reliably support text content.

Common uses include encoding email attachments, embedding image data in HTML or CSS, and transferring complex data in API calls.

History and Origin

Base64 was first introduced in the early days of email systems, particularly with the MIME (Multipurpose Internet Mail Extensions) standard in 1992. The need arose because early email protocols could only reliably transmit 7-bit ASCII characters, while binary files like images or executables required all 8 bits. Base64 solved this problem by encoding 8-bit data into a subset of ASCII characters.

How Base64 Works

Base64 encoding transforms binary data by dividing it into 6-bit chunks, each representing one of 64 possible values (0-63). These values are then represented by ASCII characters (A-Z, a-z, 0-9, + and /). The equals sign (=) is used for padding at the end if needed.

The encoding process takes 3 bytes (24 bits) of binary data at a time and converts them into 4 Base64 characters. If the input data length is not divisible by 3, padding characters are added to ensure the output length is a multiple of 4 characters.

Character Set

The standard Base64 alphabet uses the following 64 characters:

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

Variants and URL Safety

Several variants of Base64 exist for specific use cases. URL-safe Base64 replaces + and / with - and _ to avoid issues with URL encoding. Other variants like Base64URL, Radix-64, and others exist for specific applications like PEM encryption and OpenPGP.

Encoding Process Example

For example, to encode the ASCII text "Man":

  1. Convert to ASCII values: M = 77, a = 97, n = 110
  2. Convert to binary: 01001101 01100001 01101110
  3. Group into 6-bit chunks: 010011 010110 000101 101110
  4. Convert to decimal: 19, 22, 5, 46
  5. Convert to Base64 characters: T, W, F, u
So, "Man" becomes "TWFu" in Base64 encoding.

Padding Explanation

If the number of bytes to encode is not divisible by 3, padding is used to maintain proper alignment. The padding character "=" has no data significance; it simply indicates that fewer than 24 bits are encoded in the final Base64 block. One "=" means that the final block encodes just 16 bits, while "==" means it encodes just 8 bits.

Efficiency

Base64 encoding increases the data size by approximately 33% compared to the original binary data (specifically, the output is 4/3 times the size of the input). This overhead is the trade-off for ensuring data integrity across text-only systems.

Common Use Cases

  • Email attachments (MIME)
  • Data URIs in web development (e.g., data:image/png;base64,iVBOR...)
  • Embedding images directly in HTML/CSS
  • Storing binary data in JSON
  • HTTP Basic Authentication
  • SSL certificates
  • Cookies and web storage that need to contain binary data
  • JWT (JSON Web Tokens)
  • Digital signatures and encryption

Limitations

While Base64 is useful, it has limitations:

  • Increased size (approximately 33% larger)
  • Not meant for encryption or security (just encoding)
  • Some environments may require special handling for certain Base64 characters
  • Line length limitations in some systems (like email requiring breaks at 76 characters)

Operation successful