-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathBase64Util.cs
137 lines (125 loc) · 4.29 KB
/
Base64Util.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Text;
namespace EasyTool
{
/// <summary>
/// Base64 编码解码工具类
/// </summary>
public static class Base64Util
{
// Base64 字符集,共 64 个字符
private static readonly char[] BASE64_CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".ToCharArray();
// Base64 填充字符
private const char BASE64_PADDING_CHAR = '=';
/// <summary>
/// 将给定的字节数组转换为 Base64 编码字符串。
/// </summary>
/// <param name="bytes">要转换的字节数组</param>
/// <returns>转换后的 Base64 编码字符串</returns>
public static string Encode(byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException(nameof(bytes));
}
int length = bytes.Length;
if (length == 0)
{
return string.Empty;
}
char[] chars = new char[(length + 2) / 3 * 4];
int index = 0;
for (int i = 0; i < length; i += 3)
{
int val = (bytes[i] << 16) + ((i + 1 < length ? bytes[i + 1] : 0) << 8) + (i + 2 < length ? bytes[i + 2] : 0);
chars[index++] = BASE64_CHARS[(val >> 18) & 0x3F];
chars[index++] = BASE64_CHARS[(val >> 12) & 0x3F];
chars[index++] = BASE64_CHARS[(val >> 6) & 0x3F];
chars[index++] = BASE64_CHARS[val & 0x3F];
}
// 添加填充字符
int paddingCount = length % 3;
if (paddingCount > 0)
{
chars[chars.Length - 1] = BASE64_PADDING_CHAR;
if (paddingCount == 1)
{
chars[chars.Length - 2] = BASE64_PADDING_CHAR;
}
}
return new string(chars);
}
/// <summary>
/// 将给定的 Base64 编码字符串转换为字节数组。
/// </summary>
/// <param name="str">要转换的 Base64 编码字符串</param>
/// <returns>转换后的字节数组</returns>
public static byte[] Decode(string str)
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentException("String is null or empty.", nameof(str));
}
int length = str.Length;
if (length % 4 != 0)
{
throw new ArgumentException("Invalid length of input string: " + length, nameof(str));
}
int paddingCount = 0;
if (length > 0 && str[length - 1] == BASE64_PADDING_CHAR)
{
paddingCount++;
}
if (length > 1 && str[length - 2] == BASE64_PADDING_CHAR)
{
paddingCount++;
}
byte[] bytes = new byte[length / 4 * 3 - paddingCount];
int index = 0;
for (int i = 0; i < length; i += 4)
{
int val = (DecodeBase64Char(str[i]) << 18) +
(DecodeBase64Char(str[i + 1]) << 12) +
(DecodeBase64Char(str[i + 2]) << 6) +
DecodeBase64Char(str[i + 3]);
bytes[index++] = (byte)(val >> 16);
if (index < bytes.Length)
{
bytes[index++] = (byte)(val >> 8);
}
if (index < bytes.Length)
{
bytes[index++] = (byte)val;
}
}
return bytes;
}
// 解码 Base64 字符
private static int DecodeBase64Char(char c)
{
if (c >= 'A' && c <= 'Z')
{
return c - 'A';
}
if (c >= 'a' && c <= 'z')
{
return c - 'a' + 26;
}
if (c >= '0' && c <= '9')
{
return c - '0' + 52;
}
if (c == '+')
{
return 62;
}
if (c == '/')
{
return 63;
}
throw new ArgumentException("Invalid character in input string: " + c, nameof(c));
}
}
}