标签:
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(0, soFromBeginning); SetLength(abt, sso.Size * 2); sso.ReadData(abt, sso.Size * 2); 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(0, soFromBeginning); SetLength(abt, sso.Size * 2); sso.ReadData(abt, sso.Size * 2); 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[0..63] of Char = ( #65, #66, #67, #68, #69, #70, #71, #72, #73, #74, #75, #76, #77, #78, #79, #80, #81, #82, #83, #84, #85, #86, #87, #88, #89, #90, #97, #98, #99, #100, #101, #102, #103, #104, #105, #106, #107, #108, #109, #110, #111, #112, #113, #114, #115, #116, #117, #118, #119, #120, #121, #122, #48, #49, #50, #51, #52, #53, #54, #55, #56, #57, #43, #47); const BASE64DeTable : array[43..122] of Byte = ($3E, $7F, $7F, $7F, $3F, $34, $35, $36, $37, $38, $39, $3A, $3B, $3C, $3D, $7F, $7F, $7F, $7F, $7F, $7F, $7F, $00, $01, $02, $03, $04, $05, $06, $07, $08, $09, $0A, $0B, $0C, $0D, $0E, $0F, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $7F, $7F, $7F, $7F, $7F, $7F, $1A, $1B, $1C, $1D, $1E, $1F, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $2A, $2B, $2C, $2D, $2E, $2F, $30, $31, $32, $33); //BASE64算法流加密 procedure EncodeStream(InStream, OutStream : TStream); var I, O, Count : Integer; InBuf : array[1..45] of Byte; OutBuf : array[0..62] of Char; //Char Temp : Byte; abt:TBytes; begin FillChar(OutBuf, Sizeof(OutBuf), #0); repeat Count := InStream.Read(InBuf, SizeOf(InBuf)); if Count = 0 then Break; I := 1; O := 0; while I <= (Count-2) do begin { 编码第一个字节 } Temp := (InBuf[I] shr 2); OutBuf[O] := Char(BASE64Table[Temp and $3F]); { 编码第二个字节 } Temp := (InBuf[I] shl 4) or (InBuf[I+1] shr 4); OutBuf[O+1] := Char(BASE64Table[Temp and $3F]); { 编码第三个字节 } Temp := (InBuf[I+1] shl 2) or (InBuf[I+2] shr 6); OutBuf[O+2] := Char(BASE64Table[Temp and $3F]); { 编码第四个字节 } Temp := (InBuf[I+2] and $3F); OutBuf[O+3] := Char(BASE64Table[Temp]); Inc(I, 3); Inc(O, 4); end; if (I <= Count) then begin Temp := (InBuf[I] shr 2); OutBuf[O] := Char(BASE64Table[Temp and $3F]); { 一个奇数字节 } if I = Count then begin Temp := (InBuf[I] shl 4) and $30; OutBuf[O+1] := Char(BASE64Table[Temp and $3F]); OutBuf[O+2] := ‘=‘; { 两个基数字节 } end else begin Temp := ((InBuf[I] shl 4) and $30) or ((InBuf[I+1] shr 4) and $0F); OutBuf[O+1] := Char(BASE64Table[Temp and $3F]); Temp := (InBuf[I+1] shl 2) and $3C; OutBuf[O+2] := Char(BASE64Table[Temp and $3F]); end; { 增加= } OutBuf[O+3] := ‘=‘; Inc(O, 4); end; { 把编码好的块写到流中 } OutStream.Write(OutBuf, O*2); // 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[0..87] of Byte; OutBuf : array[0..65] of Byte; begin repeat O := 0; I := 0; Count := InStream.Read(InBuf, SizeOf(InBuf)); if (Count = 0) then Break; { 解密的数据输入到流中 } while I < Count do begin if (InBuf[I] < 43) or (InBuf[I] > 122) or (InBuf[I+1] < 43) or (InBuf[I+1] > 122) or (InBuf[I+2] < 43) or (InBuf[I+2] > 122) or (InBuf[I+3] < 43) or (InBuf[I+3] > 122) then raise Exception.Create(‘Invalid Base64 Character‘); c1 := BASE64DeTable[InBuf[I]]; c2 := BASE64DeTable[InBuf[I+1]]; c3 := BASE64DeTable[InBuf[I+2]]; OutBuf[O] := ((c1 shl 2) or (c2 shr 4)); Inc(O); if Char(InBuf[I+2]) <> ‘=‘ then begin OutBuf[O] := ((c2 shl 4) or (c3 shr 2)); Inc(O); if Char(InBuf[I+3]) <> ‘=‘ then begin OutBuf[O] := ((c3 shl 6) or BASE64DeTable[InBuf[I+3]]); Inc(O); end; end; Inc(I, 4); 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[1], Length(Input)); InStream.Position := 0; EncodeStream(InStream, OutStream); OutStream.Position := 0; SetLength(Result, OutStream.Size); OutStream.Read(Result[1], 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[1], Length(Input)); InStream.Position := 0; DecodeStream(InStream, OutStream); OutStream.Position := 0; SetLength(Result, OutStream.Size); OutStream.Read(Result[1], OutStream.Size); InStream.Free; OutStream.Free; end; end.
标签:
原文地址:http://www.cnblogs.com/cb168/p/4689652.html