Base64解码: const //BaseTable BaseTable:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; ...... function FindInTable(CSource:char):integer; begin result:=Pos(string(CSource),BaseTable)-1; end;function DecodeBase64(Source:string):string; var SrcLen,Times,i:integer; x1,x2,x3,x4,xt:byte; begin result:=''; SrcLen:=Length(Source); Times:=SrcLen div 4; for i:=0 to Times-1 do begin x1:=FindInTable(Source[1+i*4]); x2:=FindInTable(Source[2+i*4]); x3:=FindInTable(Source[3+i*4]); x4:=FindInTable(Source[4+i*4]); x1:=x1 shl 2; xt:=x2 shr 4; x1:=x1 or xt; x2:=x2 shl 4; result:=result+chr(x1); if x3= 64 then break; xt:=x3 shr 2; x2:=x2 or xt; x3:=x3 shl 6; result:=result+chr(x2); if x4=64 then break; x3:=x3 or x4; result:=result+chr(x3); end; end;
还有一个: //for Delphi6{********************************************************} { } { Borland Delphi Visual Component Library } { } { Copyright (c) 2000, 2001 Borland Software Corporation } { } {********************************************************} unit EncdDecd;interfaceuses Classes;procedure EncodeStream(Input, Output: TStream); procedure DecodeStream(Input, Output: TStream); function EncodeString(const Input: string): string; function DecodeString(const Input: string): string;implementationconst EncodeTable: array[0..63] of Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789+/'; DecodeTable: array[#0..#127] of Integer = ( Byte('='), 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 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, 64, 64, 64, 64, 64, 64, 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, 64, 64, 64, 64, 64);type PPacket = ^TPacket; TPacket = packed record case Integer of 0: (b0, b1, b2, b3: Byte); 1: (i: Integer); 2: (a: array[0..3] of Byte); 3: (c: array[0..3] of Char); end;procedure EncodePacket(const Packet: TPacket; NumChars: Integer; OutBuf: PChar); begin OutBuf[0] := EnCodeTable[Packet.a[0] shr 2]; OutBuf[1] := EnCodeTable[((Packet.a[0] shl 4) or (Packet.a[1] shr 4)) and $0000003f]; if NumChars < 2 then OutBuf[2] := '=' else OutBuf[2] := EnCodeTable[((Packet.a[1] shl 2) or (Packet.a[2] shr 6)) and $0000003f]; if NumChars < 3 then OutBuf[3] := '=' else OutBuf[3] := EnCodeTable[Packet.a[2] and $0000003f]; end;function DecodePacket(InBuf: PChar; var nChars: Integer): TPacket; begin Result.a[0] := (DecodeTable[InBuf[0]] shl 2) or (DecodeTable[InBuf[1]] shr 4); NChars := 1; if InBuf[2] <> '=' then begin Inc(NChars); Result.a[1] := (DecodeTable[InBuf[1]] shl 4) or (DecodeTable[InBuf[2]] shr 2); end; if InBuf[3] <> '=' then begin Inc(NChars); Result.a[2] := (DecodeTable[InBuf[2]] shl 6) or DecodeTable[InBuf[3]]; end; end;procedure EncodeStream(Input, Output: TStream); type PInteger = ^Integer; var InBuf: array[0..509] of Byte; OutBuf: array[0..1023] of Char; BufPtr: PChar; I, J, K, BytesRead: Integer; Packet: TPacket; begin K := 0; repeat BytesRead := Input.Read(InBuf, SizeOf(InBuf)); I := 0; BufPtr := OutBuf; while I < BytesRead do begin if BytesRead - I < 3 then J := BytesRead - I else J := 3; Packet.i := 0; Packet.b0 := InBuf[I]; if J > 1 then Packet.b1 := InBuf[I + 1]; if J > 2 then Packet.b2 := InBuf[I + 2]; EncodePacket(Packet, J, BufPtr); Inc(I, 3); Inc(BufPtr, 4); Inc(K, 4); if K > 75 then begin BufPtr[0] := #$0D; BufPtr[1] := #$0A; Inc(BufPtr, 2); K := 0; end; end; Output.Write(Outbuf, BufPtr - PChar(@OutBuf)); until BytesRead = 0; end;procedure DecodeStream(Input, Output: TStream); var InBuf: array[0..75] of Char; OutBuf: array[0..49] of Byte; InBufPtr, OutBufPtr: PChar; I, J, K, BytesRead: Integer; Packet: TPacket; procedure SkipWhite; var C: Char; NumRead: Integer; begin while True do begin NumRead := Input.Read(C, 1); if NumRead = 1 then begin if C in ['0'..'9','A'..'Z','a'..'z','+','/','='] then begin Input.Position := Input.Position - 1; Break; end; end else Break; end; end;begin repeat SkipWhite; BytesRead := Input.Read(InBuf, SizeOf(InBuf)); InBufPtr := InBuf; OutBufPtr := @OutBuf; I := 0; while I < BytesRead do begin Packet := DecodePacket(InBufPtr, J); K := 0; while J > 0 do begin OutBufPtr^ := Char(Packet.a[K]); Inc(OutBufPtr); Dec(J); Inc(K); end; Inc(InBufPtr, 4); Inc(I, 4); end; Output.Write(OutBuf, OutBufPtr - PChar(@OutBuf)); until BytesRead = 0; end;function EncodeString(const Input: string): string; var I, K, J: Integer; Packet: TPacket; begin Result := ''; I := (Length(Input) div 3) * 4; if Length(Input) mod 3 > 0 then Inc(I, 4); SetLength(Result, I); J := 1; for I := 1 to Length(Input) div 3 do begin Packet.i := 0; Packet.a[0] := Byte(Input[(I - 1) * 3 + 1]); Packet.a[1] := Byte(Input[(I - 1) * 3 + 2]); Packet.a[2] := Byte(Input[(I - 1) * 3 + 3]); EncodePacket(Packet, 3, PChar(@Result[J])); Inc(J, 4); end; K := 0; Packet.i := 0; for I := Length(Input) - (Length(Input) mod 3) + 1 to Length(Input) do begin Packet.a[K] := Byte(Input[I]); Inc(K); if I = Length(Input) then EncodePacket(Packet, Length(Input) mod 3, PChar(@Result[J])); end; end;function DecodeString(const Input: string): string; var I, J, K: Integer; Packet: TPacket; begin Result := ''; for I := 1 to Length(Input) div 4 do begin Packet := DecodePacket(PChar(@Input[(I - 1) * 4 + 1]), J); K := 0; while J > 0 do begin Result := Result + Packet.c[K]; Inc(K); Dec(J); end; end; end;end.
如果是在标题中,一般格式如下:
?GB2312?B?...? //Base64编码
?GB2312?Q?...? //Quoted-Printable编码如果是正文内容,则有Content-Encoding指示是用什么编码的,
Content-Encoding:Base64 //Base64编码
Content-Encoding:Quoted-Printable //Quoted-Printable编码你可以去网上搜索一下关于这两种编码的算法
//Base64编码
//Quoted-Printable编码
const
//BaseTable
BaseTable:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
......
function FindInTable(CSource:char):integer;
begin
result:=Pos(string(CSource),BaseTable)-1;
end;function DecodeBase64(Source:string):string;
var
SrcLen,Times,i:integer;
x1,x2,x3,x4,xt:byte;
begin
result:='';
SrcLen:=Length(Source);
Times:=SrcLen div 4;
for i:=0 to Times-1 do
begin
x1:=FindInTable(Source[1+i*4]);
x2:=FindInTable(Source[2+i*4]);
x3:=FindInTable(Source[3+i*4]);
x4:=FindInTable(Source[4+i*4]);
x1:=x1 shl 2;
xt:=x2 shr 4;
x1:=x1 or xt;
x2:=x2 shl 4;
result:=result+chr(x1);
if x3= 64 then break;
xt:=x3 shr 2;
x2:=x2 or xt;
x3:=x3 shl 6;
result:=result+chr(x2);
if x4=64 then break;
x3:=x3 or x4;
result:=result+chr(x3);
end;
end;
//for Delphi6{********************************************************}
{ }
{ Borland Delphi Visual Component Library }
{ }
{ Copyright (c) 2000, 2001 Borland Software Corporation }
{ }
{********************************************************}
unit EncdDecd;interfaceuses Classes;procedure EncodeStream(Input, Output: TStream);
procedure DecodeStream(Input, Output: TStream);
function EncodeString(const Input: string): string;
function DecodeString(const Input: string): string;implementationconst
EncodeTable: array[0..63] of Char =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789+/'; DecodeTable: array[#0..#127] of Integer = (
Byte('='), 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 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, 64, 64, 64, 64, 64,
64, 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, 64, 64, 64, 64, 64);type
PPacket = ^TPacket;
TPacket = packed record
case Integer of
0: (b0, b1, b2, b3: Byte);
1: (i: Integer);
2: (a: array[0..3] of Byte);
3: (c: array[0..3] of Char);
end;procedure EncodePacket(const Packet: TPacket; NumChars: Integer; OutBuf: PChar);
begin
OutBuf[0] := EnCodeTable[Packet.a[0] shr 2];
OutBuf[1] := EnCodeTable[((Packet.a[0] shl 4) or (Packet.a[1] shr 4)) and $0000003f];
if NumChars < 2 then
OutBuf[2] := '='
else OutBuf[2] := EnCodeTable[((Packet.a[1] shl 2) or (Packet.a[2] shr 6)) and $0000003f];
if NumChars < 3 then
OutBuf[3] := '='
else OutBuf[3] := EnCodeTable[Packet.a[2] and $0000003f];
end;function DecodePacket(InBuf: PChar; var nChars: Integer): TPacket;
begin
Result.a[0] := (DecodeTable[InBuf[0]] shl 2) or
(DecodeTable[InBuf[1]] shr 4);
NChars := 1;
if InBuf[2] <> '=' then
begin
Inc(NChars);
Result.a[1] := (DecodeTable[InBuf[1]] shl 4) or
(DecodeTable[InBuf[2]] shr 2);
end;
if InBuf[3] <> '=' then
begin
Inc(NChars);
Result.a[2] := (DecodeTable[InBuf[2]] shl 6) or DecodeTable[InBuf[3]];
end;
end;procedure EncodeStream(Input, Output: TStream);
type
PInteger = ^Integer;
var
InBuf: array[0..509] of Byte;
OutBuf: array[0..1023] of Char;
BufPtr: PChar;
I, J, K, BytesRead: Integer;
Packet: TPacket;
begin
K := 0;
repeat
BytesRead := Input.Read(InBuf, SizeOf(InBuf));
I := 0;
BufPtr := OutBuf;
while I < BytesRead do
begin
if BytesRead - I < 3 then
J := BytesRead - I
else J := 3;
Packet.i := 0;
Packet.b0 := InBuf[I];
if J > 1 then
Packet.b1 := InBuf[I + 1];
if J > 2 then
Packet.b2 := InBuf[I + 2];
EncodePacket(Packet, J, BufPtr);
Inc(I, 3);
Inc(BufPtr, 4);
Inc(K, 4);
if K > 75 then
begin
BufPtr[0] := #$0D;
BufPtr[1] := #$0A;
Inc(BufPtr, 2);
K := 0;
end;
end;
Output.Write(Outbuf, BufPtr - PChar(@OutBuf));
until BytesRead = 0;
end;procedure DecodeStream(Input, Output: TStream);
var
InBuf: array[0..75] of Char;
OutBuf: array[0..49] of Byte;
InBufPtr, OutBufPtr: PChar;
I, J, K, BytesRead: Integer;
Packet: TPacket; procedure SkipWhite;
var
C: Char;
NumRead: Integer;
begin
while True do
begin
NumRead := Input.Read(C, 1);
if NumRead = 1 then
begin
if C in ['0'..'9','A'..'Z','a'..'z','+','/','='] then
begin
Input.Position := Input.Position - 1;
Break;
end;
end else Break;
end;
end;begin
repeat
SkipWhite;
BytesRead := Input.Read(InBuf, SizeOf(InBuf));
InBufPtr := InBuf;
OutBufPtr := @OutBuf;
I := 0;
while I < BytesRead do
begin
Packet := DecodePacket(InBufPtr, J);
K := 0;
while J > 0 do
begin
OutBufPtr^ := Char(Packet.a[K]);
Inc(OutBufPtr);
Dec(J);
Inc(K);
end;
Inc(InBufPtr, 4);
Inc(I, 4);
end;
Output.Write(OutBuf, OutBufPtr - PChar(@OutBuf));
until BytesRead = 0;
end;function EncodeString(const Input: string): string;
var
I, K, J: Integer;
Packet: TPacket;
begin
Result := '';
I := (Length(Input) div 3) * 4;
if Length(Input) mod 3 > 0 then Inc(I, 4);
SetLength(Result, I);
J := 1;
for I := 1 to Length(Input) div 3 do
begin
Packet.i := 0;
Packet.a[0] := Byte(Input[(I - 1) * 3 + 1]);
Packet.a[1] := Byte(Input[(I - 1) * 3 + 2]);
Packet.a[2] := Byte(Input[(I - 1) * 3 + 3]);
EncodePacket(Packet, 3, PChar(@Result[J]));
Inc(J, 4);
end;
K := 0;
Packet.i := 0;
for I := Length(Input) - (Length(Input) mod 3) + 1 to Length(Input) do
begin
Packet.a[K] := Byte(Input[I]);
Inc(K);
if I = Length(Input) then
EncodePacket(Packet, Length(Input) mod 3, PChar(@Result[J]));
end;
end;function DecodeString(const Input: string): string;
var
I, J, K: Integer;
Packet: TPacket;
begin
Result := '';
for I := 1 to Length(Input) div 4 do
begin
Packet := DecodePacket(PChar(@Input[(I - 1) * 4 + 1]), J);
K := 0;
while J > 0 do
begin
Result := Result + Packet.c[K];
Inc(K);
Dec(J);
end;
end;
end;end.