简介
标准的Base64并不适合直接放在URL里传输,因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式,而这些“%”号在存入数据库时还需要再进行转换,因为ANSI SQL中已将“%”号用作通配符。
为解决此问题,可采用一种用于URL的改进Base64编码,它在末尾填充'='号,并将标准Base64中的“+”和“/”分别改成了“-”和“_”,这样就免去了在URL编解码和数据库存储时所要作的转换,避免了编码信息长度在此过程中的增加,并统一了数据库、表单等处对象标识符的格式。
另有一种用于正则表达式的改进Base64变种,它将“+”和“/”改成了“!”和“-”,因为“+”,“*”以及前面在IRCu中用到的“[”和“]”在正则表达式中都可能具有特殊含义。
此外还有一些变种,它们将“+/”改为“_-”或“._”(用作编程语言中的标识符名称)或“.-”(用于XML中的Nmtoken)甚至“_:”(用于XML中的Name)。
Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8=4*6=24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3。
规则
关于这个编码的规则:
①.把3个字节变成4个字节。
②每76个字符加一个换行符。
③.最后的结束符也要处理。
例子1
转换前 11111111, 11111111, 11111111 (二进制)
转换后 00111111, 00111111, 00111111, 00111111 (二进制)
上面的三个字节是原文,下面的四个字节是转换后的Base64编码,其前两位均为0。
转换后,我们用一个码表来得到我们想要的字符串(也就是最终的Base64编码),这个表是这样的:(摘自RFC2045)
转换表
Table 1: The Base64 Alphabet
索引
对应字符
|
0
A
|
1
B
|
2
C
|
3
D
|
4
E
|
5
F
|
6
G
|
7
H
|
8
I
|
9
J
|
10
K
|
11
L
|
12
M
|
13
N
|
14
O
|
15
P
|
16
Q
|
17
R
|
18
S
|
19
T
|
20
U
|
21
V
|
22
W
|
23
X
|
24
Y
|
25
Z
|
26
a
|
27
b
|
28
c
|
29
d
|
30
e
|
31
f
|
32
g
|
33
h
|
34
i
|
35
j
|
36
k
|
37
l
|
38
m
|
39
n
|
40
o
|
41
p
|
42
q
|
43
r
|
44
s
|
45
t
|
46
u
|
47
v
|
48
w
|
49
x
|
50
y
|
51
z
|
52
0
|
53
1
|
54
2
|
55
3
|
56
4
|
57
5
|
58
6
|
59
7
|
60
8
|
61
9
|
62
+
|
63
/
|
例子2
转换前 10101101,10111010,01110110
转换后 00101011, 00011011 ,00101001 ,00110110
十进制 43 27 41 54
对应码表中的值 r b p 2
所以上面的24位编码,编码后的Base64值为rbp2
解码同理,把rbq2的二进制位连接上再重组得到三个8位值,得出原码。
(解码只是编码的逆过程,有关MIME的RFC还有很多,如果需要详细情况请自行查找。)
第一个字节,根据源字节的第一个字节处理。
规则:源第一字节右移两位,去掉低2位,高2位补零。
既:00+高6位
第二个字节,根据源字节的第一个字节和第二个字节联合处理。
规则如下,第一个字节高6位去掉然后左移四位,第二个字节右移四位
即:源第一字节低2位+源第2字节高4位
第三个字节,根据源字节的第二个字节和第三个字节联合处理,
规则第二个字节去掉高4位并左移两位(得高6位),第三个字节右移6位并去掉高6位(得低2位),相加即可
第四个字节,规则,源第三字节去掉高2位即可
//用更接近于编程的思维来说,编码的过程是这样的:
//第一个字符通过右移2位获得第一个目标字符的Base64表位置,根据这个数值取到表上相应的字符,就是第一//个目标字符。
//然后将第一个字符与0x03(00000011)进行与(&)操作并左移4位,接着第二个字符右移4位与前者相或(|),即获得第二个目标字符。
//再将第二个字符与0x0f(00001111)进行与(&)操作并左移2位,接着第三个字符右移6位与前者相或(|),获得第三个目标字符。
//最后将第三个字符与0x3f(00111111)进行与(&)操作即获得第四个目标字符。
//在以上的每一个步骤之后,再把结果与0x3F进行AND位操作,就可以得到编码后的字符了。
原文的字节数量应该是3的倍数,如果这个条件不能满足的话,具体的解决办法是这样的:原文剩余的字节根据编码规则继续单独转(1变2,2变3;不够的位数用0补全),再用=号补满4个字节。这就是为什么有些Base64编码会以一个或两个等号结束的原因,但等号最多只有两个。因为一个原字节至少会变成两个目标字节,所以余数任何情况下都只可能是0,1,2这三个数中的一个。如果余数是0的话,就表示原文字节数正好是3的倍数(最理想的情况)。如果是1的话,转成2个Base64编码字符,为了让Base64编码是4的倍数,就要补2个等号;同理,如果是2的话,就要补1个等号。
原理
转码过程例子:
3*8=4*6
内存1个字节占8位
转前: s 1 3
先转成ascii:对应 115 49 51
2进制:01110011 00110001 00110011
6个一组(4组) 011100110011000100110011
然后才有后面的 011100 110011 000100 110011
然后计算机一个字节占8位,不够就自动补两个高位0了
所以有了高位补0
科学计算器输入 00011100 00110011 00000100 00110011
得到 28 51 4 51
查对下照表 c z E z
先以“迅雷下载”为例:很多下载类网站都提供“迅雷下载”的链接,其地址通常是加密的迅雷专用下载地址。
其实迅雷的“专用地址”也是用Base64"加密"的,其过程如下:
一、在地址的前后分别添加AA和ZZ
二、对新的字符串进行Base64编码
另:Flashget的与迅雷类似,只不过在第一步时加的“料”不同罢了,Flashget在地址前后加的“料”是[FLASHGET]
而QQ旋风的干脆不加料,直接就对地址进行Base64编码了
应用
Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的一个标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到。
然而,标准的Base64并不适合直接放在URL里传输,因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式,而这些“%”号在存入数据库时还需要再进行转换,因为ANSI SQL中已将“%”号用作通配符。
为解决此问题,可采用一种用于URL的改进Base64编码,它不仅在末尾去掉填充的'='号,并将标准Base64中的“+”和“/”分别改成了“-”和“_”,这样就免去了在URL编解码和数据库存储时所要作的转换,避免了编码信息长度在此过程中的增加,并统一了数据库、表单等处对象标识符的格式。
另有一种用于正则表达式的改进Base64变种,它将“+”和“/”改成了“!”和“-”,因为“+”,“/”以及前面在IRCu中用到的“[”和“]”在正则表达式中都可能具有特殊含义。
此外还有一些变种,它们将“+/”改为“_-”或“._”(用作编程语言中的标识符名称)或“.-”(用于XML中的Nmtoken)甚至“_:”(用于XML中的Name)。
其他应用
Mozilla Thunderbird和Evolution用Base64来保密电子邮件密码
Base64 也会经常用作一个简单的“加密”来保护某些数据,而真正的加密通常都比较繁琐。
垃圾讯息传播者用Base64来避过反垃圾邮件工具,因为那些工具通常都不会翻译Base64的讯息。
在LDIF档案,Base64用作编码字串。
代码实现
JavaScript版
1
if (!Shotgun)
|
2
var Shotgun = {};
|
3
if (!Shotgun.Js)
|
4
Shotgun.Js = {};
|
5
Shotgun.Js.Base64 = {
|
6
_table: [
|
7
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
8
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
9
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
10
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
|
11
],
|
12
|
13
encode: function (bin) {
|
14
var codes = [];
|
15
var un = 0;
|
16
un = bin.length % 3;
|
17
if (un == 1)
|
18
bin.push(0, 0);
|
19
else if (un == 2)
|
20
bin.push(0);
|
21
for (var i = 2; i < bin.length; i += 3) {
|
22
var c = bin[i - 2] << 16;
|
23
c |= bin[i - 1] << 8;
|
24
c |= bin[i];
|
25
codes.push(this._table[c >> 18 & 0x3f]);
|
26
codes.push(this._table[c >> 12 & 0x3f]);
|
27
codes.push(this._table[c >> 6 & 0x3f]);
|
28
codes.push(this._table[c & 0x3f]);
|
29
}
|
30
if (un >= 1) {
|
31
codes[codes.length - 1] = "=";
|
32
bin.pop();
|
33
}
|
34
if (un == 1) {
|
35
codes[codes.length - 2] = "=";
|
36
bin.pop();
|
37
}
|
38
return codes.join("");
|
39
},
|
40
decode: function (base64Str) {
|
41
var i = 0;
|
42
var bin = [];
|
43
var x = 0, code = 0, eq = 0;
|
44
while (i < base64Str.length) {
|
45
var c = base64Str.charAt(i++);
|
46
var idx = this._table.indexOf(c);
|
47
if (idx == -1) {
|
48
switch (c) {
|
49
case '=': idx = 0; eq++; break;
|
50
case ' ':
|
51
case 'n':
|
52
case "r":
|
53
case 't':
|
54
continue;
|
55
default:
|
56
throw { "message": "u0062u0061u0073u0065u0036u0034u002Eu0074u0068u0065u002Du0078u002Eu0063u006Eu0020u0045u0072u0072u006Fu0072u003Au65E0u6548u7F16u7801uFF1A" + c };
|
57
}
|
58
}
|
59
if (eq > 0 && idx != 0)
|
60
throw { "message": "u0062u0061u0073u0065u0036u0034u002Eu0074u0068u0065u002Du0078u002Eu0063u006Eu0020u0045u0072u0072u006Fu0072u003Au7F16u7801u683Cu5F0Fu9519u8BEFuFF01" };
|
61
|
62
code = code << 6 | idx;
|
63
if (++x != 4)
|
64
continue;
|
65
bin.push(code >> 16);
|
66
bin.push(code >> 8 & 0xff);
|
67
bin.push(code & 0xff)
|
68
code = x = 0;
|
69
}
|
70
if (code != 0)
|
71
throw { "message": "u0062u0061u0073u0065u0036u0034u002Eu0074u0068u0065u002Du0078u002Eu0063u006Eu0020u0045u0072u0072u006Fu0072u003Au7F16u7801u6570u636Eu957Fu5EA6u9519u8BEF" };
|
72
if (eq == 1)
|
73
bin.pop();
|
74
else if (eq == 2) {
|
75
bin.pop();
|
76
bin.pop();
|
77
} else if (eq > 2)
|
78
throw { "message": "u0062u0061u0073u0065u0036u0034u002Eu0074u0068u0065u002Du0078u002Eu0063u006Eu0020u0045u0072u0072u006Fu0072u003Au7F16u7801u683Cu5F0Fu9519u8BEFuFF01" };
|
79
|
80
return bin;
|
81
}
|
82
};
|
BASH版
1
base64Table=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + /);
|
2
|
3
function str2binary() {
|
4
idx=0;
|
5
for((i=0; i<${#str}; i++)); do
|
6
dividend=$(printf "%d" "'${str:i:1}");
|
7
for((j=0;j<8;j++)); do
|
8
let idx=8*i+7-j;
|
9
let bin[$idx]=$dividend%2;
|
10
dividend=$dividend/2;
|
11
done;
|
12
done;
|
13
let idx=${#str}*8;
|
14
for((i=0; i
|
15
let bin[$idx]=0;
|
16
let idx++;
|
17
done;
|
18
}
|
19
function calcBase64() {
|
20
for((i=0; i<${#bin[*]}/6; i++)); do
|
21
sum=0;
|
22
for((j=0; j<6; j++)); do
|
23
let idx=i*6+j;
|
24
let n=6-1-j;
|
25
let sum=sum+${bin[$idx]}*2**n;
|
26
done;
|
27
echo -n ${base64Table[$sum]};
|
28
done
|
29
}
|
30
|
31
declare -a bin
|
32
function base64Encode() {
|
33
read -p "please enter ASCII string:" str;
|
34
let appendZero=${#str}*8%6;
|
35
let bits=${#str}*8;
|
36
appendEqualCnt=0;
|
37
if [[ $appendZero -ne 0 ]]; then
|
38
let appendEqualCnt=(6-$appendZero)/2;
|
39
fi
|
40
str2binary;
|
41
calcBase64;
|
42
if [[ $appendEqualCnt -eq 2 ]]; then
|
43
echo -n "==";
|
44
elif [[ $appendEqualCnt -eq 1 ]]; then
|
45
echo -n "=";
|
46
fi
|
47
echo;
|
48
|
49
}
|
Java版
1
import java.util.Base64;
|
2
对于标准的Base64:
|
3
加密为字符串使用Base64.getEncoder().encodeToString();
|
4
加密为字节数组使用Base64.getEncoder().encode();
|
5
解密使用Base64.geTDecoder().decode();
|
6
对于URL安全或MIME的Base64,只需将上述getEncoder()getDecoder()更换为getUrlEncoder()getUrlDecoder()
|
7
或getMimeEncoder()和getMimeDecoder()即可。
|
PHP版
[下列代码仅在GBK中实现,UTF8代码请把if($button=="迅雷地址->普通地址") echo substr(base64_decode(str_ireplace("thunder://","",$txt1)),2,-2);这句改为if($button=="迅雷地址->普通地址") echo substr(mb_convert_encoding(base64_decode(str_ireplace("thunder://","",$txt1))),2,-2);并把charset=gb2312改为charset=utf-8]
1
|
2
$txt1=trim($_POST['text1']);
|
3
$txt2=trim($_POST['text2']);
|
4
$txt3=trim($_POST['text3']);
|
5
$button=$_POST['button'];
|
6
?>
|
7
|
8
|
9
|
10
迅雷和FlashGet,QQ旋风地址地址转换工具
|
11
|
12
|
13
|
14
|
15
|
16
|
17
迅雷转换
|
18
转换地址:
|
19
"type="text"style="width:516px;"/>
|
20
转换后的:
|
21
|
22
if($button=="普通地址->迅雷地址")echo"thunder://".base64_encode("AA".$txt1."ZZ");
|
23
if($button=="迅雷地址->普通地址")echosubstr(base64_decode(str_ireplace("thunder://","",$txt1)),2,-2);
|
24
?>"style="width:516px;"/>
|
25
|
26
迅雷地址"/>
|
27
普通地址"/>
|
28
FlashGet转换
|
29
FlashGet地址:
|
30
"type="text"style="width:516px;"/>
|
31
转换后地址:
|
32
|
33
if($button=="普通地址->FlashGet地址")echo"flashget://".base64_encode($txt2);
|
34
if($button=="FlashGet地址->普通地址")echostr_ireplace("[FLASHGET]","",base64_decode(str_ireplace("flashget://","",$txt2)));
|
35
?>"style="width:516px;"/>
|
36
|
37
FlashGet地址"name="button"/>
|
38
普通地址"name="button"/>
|
39
QQ旋风转换
|
40
QQ旋风地址:
|
41
"type="text"style="width:516px;"/>
|
42
转换后地址:
|
43
|
44
if($button=="普通地址->QQ旋风")echo"qqdl://".base64_encode($txt3);
|
45
if($button=="QQ旋风->普通地址")echobase64_decode(str_ireplace("qqdl://","",$txt3));
|
46
?>"style="width:516px;"/>
|
47
|
48
QQ旋风"name="button"/>
|
49
普通地址"name="button"/>
|
50
|
51
|
52
|
VB版
注:其中DigestStrToHexStr为可在程序外部调用加密函数
1
Option Explicit
|
2
'B as e64Encoding/DecodingAlgorithm
|
3
'By:DavidMidkif f(mznull@earthlink.net)
|
4
'
|
5
'Thisalgorithmsencodes and decodesdatain to B as e64
|
6
'for mat.Thisfor matisextremelymoreefficientthan
|
7
'Hexadecimalencoding.
|
8
Private m_bytIndex(0 To 63) As Byte
|
9
Private m_bytReverseIndex(0 To 255) As Byte
|
10
Private Const k_bytEqualSign As Byte = 61
|
11
Private Const k_bytmask1 As Byte = 3
|
12
Private Const k_bytmask2 As Byte = 15
|
13
Private Const k_bytmask3 As Byte = 63
|
14
Private Const k_bytmask4 As Byte = 192
|
15
Private Const k_bytmask5 As Byte = 240
|
16
Private Const k_bytmask6 As Byte = 252
|
17
Private Const k_bytShift2 As Byte = 4
|
18
Private Const k_bytShift4 As Byte = 16
|
19
Private Const k_bytShift6 As Byte = 64
|
20
Private Const k_lMaxBytesPerLine As Long = 152
|
21
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
|
22
Public Function Decode64(sInput As String) As String
|
23
If sInput = "" Then Exit Function
|
24
Decode64 = StrConv(DecodeArray64(sInput), vbUnicode)
|
25
End Function
|
26
Private Function DecodeArray64(sInput As String) As Byte()
|
27
Dim bytInput() As Byte
|
28
Dim bytWorkspace() As Byte
|
29
Dim bytResult() As Byte
|
30
Dim lInputCounter As Long
|
31
Dim lWorkspaceCounter As Long
|
32
bytInput = Replace(Replace(sInput, vbCrLf, ""), "=", "")
|
33
ReDim bytWorkspace(LBound(bytInput) To (UBound(bytInput) * 2)) As Byte
|
34
lWorkspaceCounter = LBound(bytWorkspace)
|
35
For lInputCounter = LBound(bytInput) To UBound(bytInput)
|
36
bytInput(lInputCounter) = m_bytReverseIndex(bytInput(lInputCounter))
|
37
Next lInputCounter
|
38
For lInputCounter = LBound(bytInput) To (UBound(bytInput) - ((UBound(bytInput) Mod 8) + 8)) Step 8
|
39
bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) k_bytShift4)
|
40
bytWorkspace(lWorkspaceCounter + 1) = ((bytInput(lInputCounter + 2) And k_bytmask2) * k_bytShift4) + (bytInput(lInputCounter + 4) k_bytShift2)
|
41
bytWorkspace(lWorkspaceCounter + 2) = ((bytInput(lInputCounter + 4) And k_bytmask1) * k_bytShift6) + bytInput(lInputCounter + 6)
|
42
lWorkspaceCounter = lWorkspaceCounter + 3
|
43
Next lInputCounter
|
44
Select Case (UBound(bytInput) Mod 8):
|
45
Case 3:
|
46
bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) k_bytShift4)
|
47
Case 5:
|
48
bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) k_bytShift4)
|
49
bytWorkspace(lWorkspaceCounter + 1) = ((bytInput(lInputCounter + 2) And k_bytmask2) * k_bytShift4) + (bytInput(lInputCounter + 4) k_bytShift2)
|
50
lWorkspaceCounter = lWorkspaceCounter + 1
|
51
Case 7:
|
52
bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) k_bytShift4)
|
53
bytWorkspace(lWorkspaceCounter + 1) = ((bytInput(lInputCounter + 2) And k_bytmask2) * k_bytShift4) + (bytInput(lInputCounter + 4) k_bytShift2)
|
54
bytWorkspace(lWorkspaceCounter + 2) = ((bytInput(lInputCounter + 4) And k_bytmask1) * k_bytShift6) + bytInput(lInputCounter + 6)
|
55
lWorkspaceCounter = lWorkspaceCounter + 2
|
56
End Select
|
57
ReDim bytResult(LBound(bytWorkspace) To lWorkspaceCounter) As Byte
|
58
If LBound(bytWorkspace) = 0 Then lWorkspaceCounter = lWorkspaceCounter + 1
|
59
CopyMemoryVarPtr (bytResult(LBound(bytResult))), VarPtr(bytWorkspace(LBound(bytWorkspace))), lWorkspaceCounter
|
60
DecodeArray64 = bytResult
|
61
End Function
|
62
Public Function Encode64(ByRefsInput As String) As String
|
63
If sInput = "" Then Exit Function
|
64
Dim bytTemp() As Byte
|
65
bytTemp = StrConv(sInput, vbFromUnicode)
|
66
Encode64 = EncodeArray64(bytTemp)
|
67
End Function
|
68
Private Function EncodeArray64(ByRefbytInput() As Byte) As String
|
69
On Error GoTo ErrorHandler
|
70
Dim bytWorkspace() As Byte, bytResult() As Byte
|
71
Dim bytCrLf(0 To 3) As Byte, lCounter As Long
|
72
Dim lWorkspaceCounter As Long, lLineCounter As Long
|
73
Dim lCompleteLines As Long, lBytesRemaining As Long
|
74
Dim lpWorkSpace As Long, lpResult As Long
|
75
Dim lpCrLf As Long
|
76
If UBound(bytInput) < 1024 Then
|
77
ReDim bytWorkspace(LBound(bytInput) To (LBound(bytInput) + 4096)) As Byte
|
78
Else
|
79
ReDim bytWorkspace(LBound(bytInput) To (UBound(bytInput) * 4)) As Byte
|
80
End If
|
81
lWorkspaceCounter = LBound(bytWorkspace)
|
82
For lCounter = LBound(bytInput) To (UBound(bytInput) - ((UBound(bytInput) Mod 3) + 3)) Step 3
|
83
bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) k_bytShift2))
|
84
bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex(((bytInput(lCounter) And k_bytmask1) * k_bytShift4) + ((bytInput(lCounter + 1)) k_bytShift4))
|
85
bytWorkspace(lWorkspaceCounter + 4) = m_bytIndex(((bytInput(lCounter + 1) And k_bytmask2) * k_bytShift2) + (bytInput(lCounter + 2) k_bytShift6))
|
86
bytWorkspace(lWorkspaceCounter + 6) = m_bytIndex(bytInput(lCounter + 2) And k_bytmask3)
|
87
lWorkspaceCounter = lWorkspaceCounter + 8
|
88
Next lCounter
|
89
Select Case (UBound(bytInput) Mod 3):
|
90
Case 0:
|
91
bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) k_bytShift2))
|
92
bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex((bytInput(lCounter) And k_bytmask1) * k_bytShift4)
|
93
bytWorkspace(lWorkspaceCounter + 4) = k_bytEqualSign
|
94
bytWorkspace(lWorkspaceCounter + 6) = k_bytEqualSign
|
95
Case 1:
|
96
bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) k_bytShift2))
|
97
bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex(((bytInput(lCounter) And k_bytmask1) * k_bytShift4) + ((bytInput(lCounter + 1)) k_bytShift4))
|
98
bytWorkspace(lWorkspaceCounter + 4) = m_bytIndex((bytInput(lCounter + 1) And k_bytmask2) * k_bytShift2)
|
99
bytWorkspace(lWorkspaceCounter + 6) = k_bytEqualSign
|
100
Case 2:
|
101
bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) k_bytShift2))
|
102
bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex(((bytInput(lCounter) And k_bytmask1) * k_bytShift4) + ((bytInput(lCounter + 1)) k_bytShift4))
|
103
bytWorkspace(lWorkspaceCounter + 4) = m_bytIndex(((bytInput(lCounter + 1) And k_bytmask2) * k_bytShift2) + ((bytInput(lCounter + 2)) k_bytShift6))
|
104
bytWorkspace(lWorkspaceCounter + 6) = m_bytIndex(bytInput(lCounter + 2) And k_bytmask3)
|
105
End Select
|
106
lWorkspaceCounter = lWorkspaceCounter + 8
|
107
If lWorkspaceCounter <= k_lMaxBytesPerLine Then
|
108
EncodeArray64 = Left$(bytWorkspace, InStr(1, bytWorkspace, Chr$(0)) - 1)
|
109
Else
|
110
bytCrLf(0) = 13
|
111
bytCrLf(1) = 0
|
112
bytCrLf(2) = 10
|
113
bytCrLf(3) = 0
|
114
ReDim bytResult(LBound(bytWorkspace) To UBound(bytWorkspace))
|
115
lpWorkSpace = VarPtr(bytWorkspace(LBound(bytWorkspace)))
|
116
lpResult = VarPtr(bytResult(LBound(bytResult)))
|
117
lpCrLf = VarPtr(bytCrLf(LBound(bytCrLf)))
|
118
lCompleteLines = Fix(lWorkspaceCounter / k_lMaxBytesPerLine)
|
119
For lLineCounter = 0 To lCompleteLines
|
120
CopyMemorylpResult , lpWorkSpace, k_lMaxBytesPerLine
|
121
lpWorkSpace = lpWorkSpace + k_lMaxBytesPerLine
|
122
lpResult = lpResult + k_lMaxBytesPerLine
|
123
CopyMemorylpResult , lpCrLf, 4&
|
124
lpResult = lpResult + 4&
|
125
Next lLineCounter
|
126
lBytesRemaining = lWorkspaceCounter - (lCompleteLines * k_lMaxBytesPerLine)
|
127
If lBytesRemaining > 0 Then CopyMemorylpResult , lpWorkSpace, lBytesRemaining
|
128
EncodeArray64 = Left$(bytResult, InStr(1, bytResult, Chr$(0)) - 1)
|
129
End If
|
130
Exit Function
|
131
ErrorHandler:
|
132
Er As ebytResult
|
133
EncodeArray64 = bytResult
|
134
End Function
|
C#版
1
直接使用.NET中的的库类函数
|
2
方法:
|
3
///
|
4
///Base64加密
|
5
///
|
6
///
|
7
///
|
8
publicstringBase64Code(stringMessage)
|
9
{
|
10
byte[]bytes=Encoding.Default.GetBytes(Message);
|
11
returnConvert.ToBase64String(bytes);
|
12
}
|
13
///
|
14
///Base64解密
|
15
///
|
16
///
|
17
///
|
18
publicstringBase64Decode(stringMessage)
|
19
{
|
20
byte[]bytes=Convert.FromBase64String(Message);
|
21
returnEncoding.Default.GetString(bytes);
|
22
}
|
python版
1
def base(string:str)->str:
|
2
oldstr = ''
|
3
newstr = []
|
4
base = ''
|
5
base64_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
6
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
7
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
8
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/']
|
9
#把原始字符串转换为二进制,用bin转换后是0b开头的,所以把b替换了,首位补0补齐8位
|
10
for i in string:
|
11
oldstr += '{:08}'.format(int(str(bin(ord(i))).replace('0b', '')))
|
12
#把转换好的二进制按照6位一组分好,最后一组不足6位的后面补0
|
13
for j in range(0, len(oldstr), 6):
|
14
newstr.append('{:<06}'.format(oldstr[j:j + 6]))
|
15
#在base_list中找到对应的字符,拼接
|
16
for l in range(len(newstr)):
|
17
base += base64_list[int(newstr[l], 2)]
|
18
#判断base字符结尾补几个‘=’
|
19
if len(string) % 3 == 1:
|
20
base += '=='
|
21
elif len(string) % 3 == 2:
|
22
base += '='
|
23
return base
|
在MIME格式的电子邮件中,base64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本。使用时,在传输编码方式中指定base64。使用的字符包括大小写字母各26个,加上10个数字,和加号“+”,斜杠“/”,一共64个字符,等号“=”用来作为后缀用途。
完整的base64定义可见RFC1421和RFC2045。编码后的数据比原始数据略长,为原来的4/3。在电子邮件中,根据RFC822规定,每76个字符,还需要加上一个回车换行。可以估算编码后数据长度大约为原长的135.1%。
转换的时候,将三个byte的数据,先后放入一个24bit的缓冲区中,先来的byte占高位。数据不足3byte的话,于缓冲区中剩下的Bit用0补足。然后,每次取出6个bit,按照其值选择ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作为编码后的输出。不断进行,直到全部输入数据转换完成。
如果最后剩下两个输入数据,在编码结果后加1个“=”;如果最后剩下一个输入数据,编码结果后加2个“=”;如果没有剩下任何数据,就什么都不要加,这样才可以保证资料还原的正确性。
举例来说,一段引用自Thomas Hobbes's Leviathan的文句:
Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.
经过base64编码之后变成:
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=