not recommend ,only for study
procedure TForm1.Button3Click(Sender: TObject);
var
ssi, sso: TStringStream;
abt: TBytes;
s1: string;
begin
ssi := TStringStream.Create(Edit1.Text);
sso := TStringStream.Create((''));
Base64Unit.EncodeStream(ssi, sso); sso.Seek(, soFromBeginning);
SetLength(abt, sso.Size * );
sso.ReadData(abt, sso.Size * ); s1 := TEncoding.Unicode.GetString(abt);
Edit2.Text := s1;
ssi.Free;
sso.Free;
end; procedure TForm1.Button5Click(Sender: TObject);
var
ssi, sso: TStringStream;
abt: TBytes;
s1: string;
begin
ssi := TStringStream.Create(Edit2.Text);
sso := TStringStream.Create((''));
Base64Unit.DecodeStream(ssi, sso); sso.Seek(, soFromBeginning);
SetLength(abt, sso.Size * );
sso.ReadData(abt, sso.Size * ); Edit4.Text := sso.DataString;
ssi.Free;
sso.Free; end;
unit Base64Unit; interface uses
Classes, SysUtils; function Base64Encryption(const Input:String):String;
function Base64Decryption(const Input:String):String; procedure EncodeStream(InStream, OutStream : TStream);
procedure DecodeStream(InStream, OutStream : TStream); implementation const
BASE64Table : array[..] of Char = ( #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #); const
BASE64DeTable : array[..] of Byte = ($3E, $7F, $7F, $7F, $3F, $,
$, $, $, $, $, $3A, $3B, $3C, $3D, $7F, $7F, $7F, $7F,
$7F, $7F, $7F, $, $, $, $, $, $, $, $, $, $,
$0A, $0B, $0C, $0D, $0E, $0F, $, $, $, $, $, $, $,
$, $, $, $7F, $7F, $7F, $7F, $7F, $7F, $1A, $1B, $1C, $1D,
$1E, $1F, $, $, $, $, $, $, $, $, $, $, $2A,
$2B, $2C, $2D, $2E, $2F, $, $, $, $); //BASE64算法流加密
procedure EncodeStream(InStream, OutStream : TStream);
var
I, O, Count : Integer;
InBuf : array[..] of Byte;
OutBuf : array[..] of Char; //Char
Temp : Byte;
abt:TBytes;
begin
FillChar(OutBuf, Sizeof(OutBuf), #); repeat
Count := InStream.Read(InBuf, SizeOf(InBuf));
if Count = then Break;
I := ;
O := ;
while I <= (Count-) do begin
{ 编码第一个字节 }
Temp := (InBuf[I] shr );
OutBuf[O] := Char(BASE64Table[Temp and $3F]); { 编码第二个字节 }
Temp := (InBuf[I] shl ) or (InBuf[I+] shr );
OutBuf[O+] := Char(BASE64Table[Temp and $3F]); { 编码第三个字节 }
Temp := (InBuf[I+] shl ) or (InBuf[I+] shr );
OutBuf[O+] := Char(BASE64Table[Temp and $3F]); { 编码第四个字节 }
Temp := (InBuf[I+] and $3F);
OutBuf[O+] := Char(BASE64Table[Temp]); Inc(I, );
Inc(O, );
end; if (I <= Count) then begin
Temp := (InBuf[I] shr );
OutBuf[O] := Char(BASE64Table[Temp and $3F]); { 一个奇数字节 }
if I = Count then begin
Temp := (InBuf[I] shl ) and $;
OutBuf[O+] := Char(BASE64Table[Temp and $3F]);
OutBuf[O+] := '=';
{ 两个基数字节 }
end else begin
Temp := ((InBuf[I] shl ) and $) or ((InBuf[I+] shr ) and $0F);
OutBuf[O+] := Char(BASE64Table[Temp and $3F]);
Temp := (InBuf[I+] shl ) and $3C;
OutBuf[O+] := Char(BASE64Table[Temp and $3F]);
end;
{ 增加= }
OutBuf[O+] := '=';
Inc(O, );
end; { 把编码好的块写到流中 }
OutStream.Write(OutBuf, O*); // Modified by for xe8 2015-07-30 15:36:51 o > o*2
until Count < SizeOf(InBuf);
end; //BASE64算法流解密
procedure DecodeStream(InStream, OutStream : TStream);
var
I, O, Count, c1, c2, c3 : Byte;
InBuf : array[..] of Byte;
OutBuf : array[..] of Byte;
begin
repeat
O := ;
I := ; Count := InStream.Read(InBuf, SizeOf(InBuf));
if (Count = ) then
Break; { 解密的数据输入到流中 }
while I < Count do begin
if (InBuf[I] < ) or (InBuf[I] > ) or
(InBuf[I+] < ) or (InBuf[I+] > ) or
(InBuf[I+] < ) or (InBuf[I+] > ) or
(InBuf[I+] < ) or (InBuf[I+] > ) then
raise Exception.Create('Invalid Base64 Character'); c1 := BASE64DeTable[InBuf[I]];
c2 := BASE64DeTable[InBuf[I+]];
c3 := BASE64DeTable[InBuf[I+]];
OutBuf[O] := ((c1 shl ) or (c2 shr ));
Inc(O);
if Char(InBuf[I+]) <> '=' then begin
OutBuf[O] := ((c2 shl ) or (c3 shr ));
Inc(O);
if Char(InBuf[I+]) <> '=' then begin
OutBuf[O] := ((c3 shl ) or BASE64DeTable[InBuf[I+]]);
Inc(O);
end;
end;
Inc(I, );
end;
OutStream.Write(OutBuf, O);
until Count < SizeOf(InBuf);
end; //BASE64算法字符串加密
function Base64Encryption(const Input:String):String;
var
InStream : TMemoryStream;
OutStream : TMemoryStream;
begin
InStream := TMemoryStream.Create;
OutStream := TMemoryStream.Create; InStream.Write(Input[], Length(Input));
InStream.Position := ;
EncodeStream(InStream, OutStream);
OutStream.Position := ;
SetLength(Result, OutStream.Size);
OutStream.Read(Result[], OutStream.Size); InStream.Free;
OutStream.Free;
end; //BASE64算法字符串解密
function Base64Decryption(const Input:String):String;
var
InStream : TMemoryStream;
OutStream : TMemoryStream;
begin
InStream := TMemoryStream.Create;
OutStream := TMemoryStream.Create; InStream.Write(Input[], Length(Input));
InStream.Position := ;
DecodeStream(InStream, OutStream);
OutStream.Position := ;
SetLength(Result, OutStream.Size);
OutStream.Read(Result[], OutStream.Size); InStream.Free;
OutStream.Free;
end; end.