1. 简介:
这是常用的网络通信的手段,原因是: zlib 压缩可以将字符串体积明显缩小(只有较长才能体现出来),而base64可以将刚刚压缩的二进制码变成可见字符,便于在语言中进行传递及网络通信。
2. 关于zlib压缩算法的使用:
在linux 用C语言开发一般用zlib 库
在Delphi5上 可以用zlibEx version 1.2.3 july 19, 2007 ? 156 kb (因为更新的版本将不再适合Delphi5, 而着重为Delphi7及2010优化)
在其它高级语言上基本都有库函数,可以直接用
3. 关于base64算法
首先预备知识:base64算法介绍:http://baike.baidu.com/view/469071.htm
以Delphi5中为例,定义:
Base64Table : String[Base64TableLength] =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
Pad = '=';
base64算法虽然只有一种,但是有人会变码表。主要是针对加号“+”和 左斜杠“/” 根据需要替换成了别的字符, 还有的换pad的等于号成其它,要特别注意。
3. 转换前后的字符串例子:
源字符串:
{ "req_type": "T", "old_time": "1970-01-01 00:17:04", "org_code": "10101049" }
转码结果:(引号之内)
“ eJyrVlAqSi2ML6ksSFWyUlAKUdJRUMrPSYkvycwFCxhamhvoGhgCkYKBgZWhuZWBCVhJUXp8cn4KRIkBCJpYKinUAgBJlBQA
”
4. 附录:在Delphi下的base64转换与逆向转换源码
function TForm1.B64Encode(const s: string): string;
var
i,c1,c2,c3: Integer;
m,n: Integer;
const
Base64: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
begin
Result := '';
m:=1;
n:=0;
for i := 1 to (Length(s) div 3) do
begin
c1 := Ord(s[m]);
c2 := Ord(s[m+1]);
c3 := Ord(s[m+2]);
m:=m+3;
Result := Result+base64[(c1 shr 2)and $3F+1];
Result := Result+base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];
Result := Result+base64[((c2 shl 2)and $3C) or ((c3 shr 6)and $03)+1];
Result := Result+base64[c3 and $3F+1];
n:=n+4;
if(n = 76)then
begin
n:=0;
Result := Result+#13#10;
end;
end;
if (Length(s) mod 3)=1 then
begin
c1 := Ord(s[m]);
Result := Result+base64[(c1 shr 2)and $3F+1];
Result := Result+base64[(c1 shl 4)and $30+1];
Result := Result+'=';
Result := Result+'=';
end;
if (Length(s) mod 3)=2 then
begin
c1 := Ord(s[m]);
c2 := Ord(s[m+1]);
Result := Result+ base64[(c1 shr 2)and $3F+1];
Result := Result+ base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];
Result := Result+base64[(c2 shl 2)and $3C+1];
Result := Result+ '=';
end;
end;
function TForm1.B64Decode(const s: string): string;
var
i,m,n: Integer;
c1,c2,c3,c4: Integer;
const
Base64: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
begin
Result := '';
n:=1;
m:=Length(s);
if s[m]='='then m:=m-1;
if s[m]='='then m:=m-1;
for i:=1 to m div 4 do
begin
c1:=Pos(s[n],Base64)-1;
c2:=Pos(s[n+1],Base64)-1;
c3:=Pos(s[n+2],Base64)-1;
c4:=Pos(s[n+3],Base64)-1;
n:=n+4;
Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));
Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));
Result:=Result+Chr(((c3 shl 6)and $C0)or c4);
end;
if m mod 4=2 then
begin
c1:=Pos(s[n],Base64)-1;
c2:=Pos(s[n+1],Base64)-1;
Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));
end;
if m mod 4=3 then
begin
c1:=Pos(s[n],Base64)-1;
c2:=Pos(s[n+1],Base64)-1;
c3:=Pos(s[n+2],Base64)-1;
Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));
Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));
end;
end;
这是常用的网络通信的手段,原因是: zlib 压缩可以将字符串体积明显缩小(只有较长才能体现出来),而base64可以将刚刚压缩的二进制码变成可见字符,便于在语言中进行传递及网络通信。
2. 关于zlib压缩算法的使用:
在linux 用C语言开发一般用zlib 库
在Delphi5上 可以用zlibEx version 1.2.3 july 19, 2007 ? 156 kb (因为更新的版本将不再适合Delphi5, 而着重为Delphi7及2010优化)
在其它高级语言上基本都有库函数,可以直接用
3. 关于base64算法
首先预备知识:base64算法介绍:http://baike.baidu.com/view/469071.htm
以Delphi5中为例,定义:
Base64Table : String[Base64TableLength] =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
Pad = '=';
base64算法虽然只有一种,但是有人会变码表。主要是针对加号“+”和 左斜杠“/” 根据需要替换成了别的字符, 还有的换pad的等于号成其它,要特别注意。

3. 转换前后的字符串例子:
源字符串:
{ "req_type": "T", "old_time": "1970-01-01 00:17:04", "org_code": "10101049" }
转码结果:(引号之内)
“
”
4. 附录:在Delphi下的base64转换与逆向转换源码
function TForm1.B64Encode(const s: string): string;
var
i,c1,c2,c3: Integer;
m,n: Integer;
const
Base64: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
begin
Result := '';
m:=1;
n:=0;
for i := 1 to (Length(s) div 3) do
begin
c1 := Ord(s[m]);
c2 := Ord(s[m+1]);
c3 := Ord(s[m+2]);
m:=m+3;
Result := Result+base64[(c1 shr 2)and $3F+1];
Result := Result+base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];
Result := Result+base64[((c2 shl 2)and $3C) or ((c3 shr 6)and $03)+1];
Result := Result+base64[c3 and $3F+1];
n:=n+4;
if(n = 76)then
begin
n:=0;
Result := Result+#13#10;
end;
end;
if (Length(s) mod 3)=1 then
begin
c1 := Ord(s[m]);
Result := Result+base64[(c1 shr 2)and $3F+1];
Result := Result+base64[(c1 shl 4)and $30+1];
Result := Result+'=';
Result := Result+'=';
end;
if (Length(s) mod 3)=2 then
begin
c1 := Ord(s[m]);
c2 := Ord(s[m+1]);
Result := Result+ base64[(c1 shr 2)and $3F+1];
Result := Result+ base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];
Result := Result+base64[(c2 shl 2)and $3C+1];
Result := Result+ '=';
end;
end;
function TForm1.B64Decode(const s: string): string;
var
i,m,n: Integer;
c1,c2,c3,c4: Integer;
const
Base64: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
begin
Result := '';
n:=1;
m:=Length(s);
if s[m]='='then m:=m-1;
if s[m]='='then m:=m-1;
for i:=1 to m div 4 do
begin
c1:=Pos(s[n],Base64)-1;
c2:=Pos(s[n+1],Base64)-1;
c3:=Pos(s[n+2],Base64)-1;
c4:=Pos(s[n+3],Base64)-1;
n:=n+4;
Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));
Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));
Result:=Result+Chr(((c3 shl 6)and $C0)or c4);
end;
if m mod 4=2 then
begin
c1:=Pos(s[n],Base64)-1;
c2:=Pos(s[n+1],Base64)-1;
Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));
end;
if m mod 4=3 then
begin
c1:=Pos(s[n],Base64)-1;
c2:=Pos(s[n+1],Base64)-1;
c3:=Pos(s[n+2],Base64)-1;
Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));
Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));
end;
end;