编码原理:3个8bit 的字节拆分为对应4*6bits,形成4个字节,每个字节高位补两个0。如果不足,末尾添加1或2个’=‘。
解码原理:与上相反
编码过程:
如下所表示的转换表,根据转换表把要转换的字符串每个字符映射到A~Z,a ~z,0~9,+,/ 的范围中。
索引
|
对应字符
|
索引
|
对应字符
|
索引
|
对应字符
|
索引
|
对应字符
|
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
| ||
15
|
P
|
32
|
g
|
49
|
x
| ||
16
|
Q
|
33
|
h
|
50
|
y
|
解码过程:
把要解码的字符串映射到0~63 的范围,然后合并字节,形成原来的字符串。
#include<iostream>
#include<string.h>
using namespace std;
char base64EncodeChars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//64 个数
char base64DecodeChars[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1, // '=' 也就是61 返回0
-1, 0, 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, -1, -1, -1, -1, -1,
-1, 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, -1, -1, -1, -1, -1};
char* base64encode( char *str,int slen, char *encodestr){
//使用unsigned char 类型,保证右移操作为逻辑右移
unsigned char c1,c2,c3;
int i=0,j=0;
// encodestr=new char[(strlen+2)/3*4+1];
while(i<slen){
c1=str[i++];
if(i==slen){
encodestr[j++]=base64EncodeChars[c1>>2];
encodestr[j++]=base64EncodeChars[((c1 & 0x3)<<4)];
encodestr[j++]='=';
encodestr[j++]='=';
break;
}
c2=str[i++];
if(i==slen){
encodestr[j++]=base64EncodeChars[c1>>2];
encodestr[j++]=base64EncodeChars[((c1 & 0x3)<<4) | (c2>>4)];
encodestr[j++]=base64EncodeChars[((c2 & 0xf)<<2) ];
encodestr[j++]='=';
break;
}
c3=str[i++];
encodestr[j++]=base64EncodeChars[c1>>2];
encodestr[j++]=base64EncodeChars[((c1 & 0x3)<<4) | (c2>>4)];
encodestr[j++]=base64EncodeChars[((c2 & 0xf)<<2) | (c3>>6)];
encodestr[j++]=base64EncodeChars[(c3 & 0x3f)];
}
encodestr[j]=0;
return encodestr;
}
char* base64decode( char *str,int slen, char *decodestr){
unsigned char c1,c2,c3,c4;
int i=0,j=0;
while(i<slen){
c1=base64DecodeChars[str[i++] & 0xff];
c2=base64DecodeChars[str[i++] & 0xff];
c3=base64DecodeChars[str[i++] & 0xff];
c4=base64DecodeChars[str[i++] & 0xff];
if(c1==255 || c2==255 || c3==255 || c4==255)
cout<<"wrong\n";
decodestr[j++]=(c1<<2) | ((c2 & 0x30)>>4);
decodestr[j++]= ((c2 & 0xf)<<4) | ((c3 & 0x3c)>>2);
decodestr[j++]=((c3 & 0x3)<<6) | (c4 & 0x3f);
}
decodestr[j]=0;
return decodestr;
}
int main(){
char str[300];
char destr[300];
// char ch=-1;
// unsigned char c=ch;
// cout<<(int)c<<endl;
cin>>str;
while(str){
char *enchar;
enchar=new char[(strlen(str)+2)/3*4+1];
cout<<"str len="<<strlen(str)<<endl;
base64encode(str,strlen(str),enchar);
cout<<"=endcode:"<<enchar<<endl;
base64decode(enchar,strlen(enchar),destr);
cout<<"=decode:"<<destr<<endl;
if(strcmp(str,destr)==0)
cout<<"right"<<endl;
else
cout<<"wrong"<<endl;
delete []enchar;
cin>>str;
}
return 0;
}