贴出来吧,自己以前写的。不太好,不过可以用。要给分哦。
unit Pub;
interface
uses
Math,SysUtils;
function Base64CodeTab(AIndex: Byte): string;
function Base64Decode(AInputStr: string): string;
function Base64DecodeTab(AIndex: Char): Byte;
function Base64Encode(AInputStr: string): string;
function BinToInt(ABinStr: string): Integer;
function HexToBinary(AHexStr: string): string;
implementation
function Base64CodeTab(AIndex: Byte): string;
begin
Result := '';
case AIndex of
0: Result := 'A';
1: Result := 'B';
2: Result := 'C';
3: Result := 'D';
4: Result := 'E';
5: Result := 'F';
6: Result := 'G';
7: Result := 'H';
8: Result := 'I';
9: Result := 'J';
10: Result := 'K';
11: Result := 'L';
12: Result := 'M';
13: Result := 'N';
14: Result := 'O';
15: Result := 'P';
16: Result := 'Q';
17: Result := 'R';
18: Result := 'S';
19: Result := 'T';
20: Result := 'U';
21: Result := 'V';
22: Result := 'W';
23: Result := 'X';
24: Result := 'Y';
25: Result := 'Z';
26: Result := 'a';
27: Result := 'b';
28: Result := 'c';
29: Result := 'd';
30: Result := 'e';
31: Result := 'f';
32: Result := 'g';
33: Result := 'h';
34: Result := 'i';
35: Result := 'j';
36: Result := 'k';
37: Result := 'l';
38: Result := 'm';
39: Result := 'n';
40: Result := 'o';
41: Result := 'p';
42: Result := 'q';
43: Result := 'r';
44: Result := 's';
45: Result := 't';
46: Result := 'u';
47: Result := 'v';
48: Result := 'w';
49: Result := 'x';
50: Result := 'y';
51: Result := 'z';
52: Result := '0';
53: Result := '1';
54: Result := '2';
55: Result := '3';
56: Result := '4';
57: Result := '5';
58: Result := '6';
59: Result := '7';
60: Result := '8';
61: Result := '9';
62: Result := '+';
63: Result := '/';
end;
end;
function Base64Decode(AInputStr: string): string;
var
i: Integer;
ABuffer: string[24];
begin
Result := '';
while Length(AInputStr) > 4 do
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[3]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[4]),2)),3,6);
for i := 0 to 2 do
Result := Result + Chr(BinToInt(Copy(ABuffer,i*8+1,8)));
AInputStr := Copy(AInputStr,5,Length(AInputStr));
end;
case Pos('=',AInputStr) of
3:
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6);
Result := Result + Chr(BinToInt(Copy(ABuffer,1,8)));
end;
4:
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[3]),2)),3,6);
for i := 0 to 1 do
Result := Result + Chr(BinToInt(Copy(ABuffer,i*8+1,8)));
end;
0:
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[3]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[4]),2)),3,6);
for i := 0 to 2 do
Result := Result + Chr(BinToInt(Copy(ABuffer,i*8+1,8)));
end;
end;
end;
function Base64DecodeTab(AIndex: Char): Byte;
begin
Result := 0;
case AIndex of
'A': Result := 0;
'B': Result := 1;
'C': Result := 2;
'D': Result := 3;
'E': Result := 4;
'F': Result := 5;
'G': Result := 6;
'H': Result := 7;
'I': Result := 8;
'J': Result := 9;
'K': Result := 10;
'L': Result := 11;
'M': Result := 12;
'N': Result := 13;
'O': Result := 14;
'P': Result := 15;
'Q': Result := 16;
'R': Result := 17;
'S': Result := 18;
'T': Result := 19;
'U': Result := 20;
'V': Result := 21;
'W': Result := 22;
'X': Result := 23;
'Y': Result := 24;
'Z': Result := 25;
'a': Result := 26;
'b': Result := 27;
'c': Result := 28;
'd': Result := 29;
'e': Result := 30;
'f': Result := 31;
'g': Result := 32;
'h': Result := 33;
'i': Result := 34;
'j': Result := 35;
'k': Result := 36;
'l': Result := 37;
'm': Result := 38;
'n': Result := 39;
'o': Result := 40;
'p': Result := 41;
'q': Result := 42;
'r': Result := 43;
's': Result := 44;
't': Result := 45;
'u': Result := 46;
'v': Result := 47;
'w': Result := 48;
'x': Result := 49;
'y': Result := 50;
'z': Result := 51;
'0': Result := 52;
'1': Result := 53;
'2': Result := 54;
'3': Result := 55;
'4': Result := 56;
'5': Result := 57;
'6': Result := 58;
'7': Result := 59;
'8': Result := 60;
'9': Result := 61;
'+': Result := 62;
'/': Result := 63;
end;
end;
function Base64Encode(AInputStr: string): string;
var
i: Integer;
ABuffer: string[24];
begin
Result := '';
while Length(AInputStr) > 3 do
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[2]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[3]),2));
for i := 0 to 3 do
begin
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,i*6+1,6)))
end;
AInputStr := Copy(AInputStr,4,Length(AInputStr));
end;
case Length(AInputStr) of
1:
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2)) + '0000';
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,1,6))) +
Base64CodeTab(BinToInt(Copy(ABuffer,7,6))) + '==';
end;
2:
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[2]),2)) + '00';
for i := 0 to 2 do
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,i*6+1,6)));
Result := Result + '=';
end;
3:
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[2]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[3]),2));
for i := 0 to 3 do
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,i*6+1,6)))
end;
end;
end;
function BinToInt(ABinStr: string): Integer;
var
i: Integer;
begin
Result := 0;
for i:=0 to Length(ABinStr)-1 do
begin
Result := Result + StrToInt(ABinStr[i+1]) * Round(IntPower(2,Length(ABinStr)-i-1));
end;
end;
function HexToBinary(AHexStr: string): string;
var
i: Integer;
begin
Result := '';
for i:=1 to Length(AHexStr) do
begin
case AHexStr of
'0':
begin
Result := Result + '0000';
end;
'1':
begin
Result := Result + '0001';
end;
'2':
begin
Result := Result + '0010';
end;
'3':
begin
Result := Result + '0011';
end;
'4':
begin
Result := Result + '0100';
end;
'5':
begin
Result := Result + '0101';
end;
'6':
begin
Result := Result + '0110';
end;
'7':
begin
Result := Result + '0111';
end;
'8':
begin
Result := Result + '1000';
end;
'9':
begin
Result := Result + '1001';
end;
'A':
begin
Result := Result + '1010';
end;
'B':
begin
Result := Result + '1011';
end;
'C':
begin
Result := Result + '1100';
end;
'D':
begin
Result := Result + '1101';
end;
'E':
begin
Result := Result + '1110';
end;
'F':
begin
Result := Result + '1111';
end;
end;
end;
end;
end.