unit tools;interface uses Classes, SysUtils; const _Code64: String[64]=('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'); type CharSet = Set of Char; function GetEncodeBASE64String(FileName: string;OutputStream: TStringStream):Integer; function EncodeBASE64(Encoded, Decoded: TMemoryStream): Integer; function DecodeBase64(const S, Alphabet: String; const PadSet: CharSet): String; function StrToBase64(const str: string): string; function Base64ToStr(const Base64: string): string; implementation function StrToBase64(const Str: string): string; var I, J, K, Len, Len1: Integer; B3: array[0..2] of Byte; B4: array[0..3] of Byte; begin if Str = '' then begin Result := ''; Exit; end; Len := Length(Str); Len1 := ((Len + 2) div 3) shl 2; SetString(Result, nil, Len1); I := 1; J := 1; while I <= Len do begin for K := 0 to 2 do if K + I > Len then B3[K] := 0 else B3[K] := Ord(Str[K + I]); B4[0] := B3[0] shr 2; B4[1] := ((B3[0] shl 4) or (B3[1] shr 4)) and 63; B4[2] := ((B3[1] shl 2) or (B3[2] shr 6)) and 63; B4[3] := B3[2] and 63; for K := 0 to 3 do begin case B4[K] of 0..25: Result[J] := Chr(B4[K] + 65); // 'A'..'Z' 26..51: Result[J] := Chr(B4[K] + 71); // 'a'..'z'(B4[K]-26+97) 62: Result[J] := '+'; 63: Result[J] := '/'; else Result[J] := Chr(B4[K] - 4); // '0'..'9'(B4[K]-52+48) end; // Result[J] := Base64_Chars[B4[K] + 1]; Inc(J); end; Inc(I, 3); end; K := 3 - Len mod 3 - 1; if K <> 2 then for I := Len1 - K to Len1 do Result[I] := '='; end; function Base64ToStr(const Base64: string): string; var I, J, K, Len, Len1: Integer; B4: array[0..3] of Byte; begin if Base64 = '' then begin Result := ''; Exit; end; Len := Length(Base64); if Len and 3 <> 0 then raise Exception.Create('Invalid Base64 length'); Len1 := (Len shr 2) * 3; SetString(Result, nil, Len1); I := 1; J := 1; while I <= Len do begin for K := 0 to 3 do begin case Base64[I] of 'A'..'Z': B4[K] := Ord(Base64[I]) - 65; 'a'..'z': B4[K] := Ord(Base64[I]) - 71; '0'..'9': B4[K] := Ord(Base64[I]) + 4; '+': B4[K] := 62; '/': B4[K] := 63; '=': B4[K] := 0; else raise Exception.CreateFmt('#%d: Invalid char in Base64', [Ord(Base64[I])]); end; Inc(I); end; Result[J] := Chr((B4[0] shl 2) or (B4[1] shr 4)); Result[J + 1] := Chr((B4[1] shl 4) or (B4[2] shr 2)); Result[J + 2] := Chr((B4[2] shl 6) or B4[3]); Inc(J, 3); end; I := Pos('=', Base64); if I <> 0 then begin I := Len - I + 1; Delete(Result, Len1 - I + 1, I); end; end; function DecodeBase64(const S, Alphabet: String; const PadSet: CharSet): String; var F, L, M, P : Integer; B, OutPos : Byte; OutB : Array[1..3] of Byte; Lookup : Array[Char] of Byte; R : PChar; begin Assert(Length(Alphabet) = 64, 'Alphabet must contain 64 characters'); L := Length(S); P := 0; if PadSet <> [] then While (L - P > 0) and (S[L - P] in PadSet) do Inc(P); M := L - P; if M = 0 then begin Result := ''; exit; end; SetLength(Result, (M * 3) div 4); FillChar(Lookup, Sizeof(Lookup), #0); For F := 0 to 63 do Lookup[Alphabet[F + 1]] := Byte(F); R := Pointer(Result); OutPos := 0; For F := 1 to L - P do begin B := Lookup[S[F]]; Case OutPos of 0 : OutB[1] := B shl 2; 1 : begin OutB[1] := OutB[1] or (B shr 4); R^ := Char(OutB[1]); Inc(R); OutB[2] := (B shl 4) and $FF; end; 2 : begin OutB[2] := OutB[2] or (B shr 2); R^ := Char(OutB[2]); Inc(R); OutB[3] := (B shl 6) and $FF; end; 3 : begin OutB[3] := OutB[3] or B; R^ := Char(OutB[3]); Inc(R); end; end; OutPos := (OutPos + 1) mod 4; end; if (OutPos > 0) and (P = 0) then // incomplete encoding, add the partial byte if not 0 if OutB[OutPos] <> 0 then Result := Result + Char(OutB[OutPos]); end; function EncodeBASE64(Encoded, Decoded: TMemoryStream): Integer; var I: LongInt; B: array[0..2279] of Byte; J, K, L, M, Quads: Integer; Stream: string[76]; EncLine: String; begin Encoded.Clear; Stream := ''; Quads := 0; {为提高效率,每2280字节流为一组进行编码} J := Decoded.Size div 2280; Decoded.Position := 0; {对前J*2280个字节流进行编码} for I := 1 to J do begin Decoded.Read(B, 2280); for M := 0 to 39 do begin for K := 0 to 18 do begin L:= 57*M + 3*K; Stream[Quads+1] := _Code64[(B[L] div 4)+1]; Stream[Quads+2] := _Code64[(B[L] mod 4)*16 + (B[L+1] div 16)+1]; Stream[Quads+3] := _Code64[(B[L+1] mod 16)*4 + (B[L+2] div 64)+1]; Stream[Quads+4] := _Code64[B[L+2] mod 64+1]; Inc(Quads, 4); if Quads = 76 then begin Stream[0] := #76; EncLine := Stream+#13#10; Encoded.Write(EncLine[1], Length(EncLine)); Quads := 0; end; end; end; end; {对以2280为模的余数字节流进行编码} J := (Decoded.Size mod 2280) div 3; for I := 1 to J do begin Decoded.Read(B, 3); Stream[Quads+1] := _Code64[(B[0] div 4)+1]; Stream[Quads+2] := _Code64[(B[0] mod 4)*16 + (B[1] div 16)+1]; Stream[Quads+3] := _Code64[(B[1] mod 16)*4 + (B[2] div 64)+1]; Stream[Quads+4] := _Code64[B[2] mod 64+1]; Inc(Quads, 4); {每行76个字符} if Quads = 76 then begin Stream[0] := #76; EncLine := Stream+#13#10; Encoded.Write(EncLine[1], Length(EncLine)); Quads := 0; end; end; {“=”补位} if (Decoded.Size mod 3) = 2 then begin Decoded.Read(B, 2); Stream[Quads+1] := _Code64[(B[0] div 4)+1]; Stream[Quads+2] := _Code64[(B[0] mod 4)*16 + (B[1] div 16)+1]; Stream[Quads+3] := _Code64[(B[1] mod 16)*4 + 1]; Stream[Quads+4] := '='; Inc(Quads, 4); end; if (Decoded.Size mod 3) = 1 then begin Decoded.Read(B, 1); Stream[Quads+1] := _Code64[(B[0] div 4)+1]; Stream[Quads+2] := _Code64[(B[0] mod 4)*16 + 1]; Stream[Quads+3] := '='; Stream[Quads+4] := '='; Inc(Quads, 4); end; Stream[0] := Chr(Quads); if Quads > 0 then begin EncLine := Stream+#13#10; Encoded.Write(EncLine[1], Length(EncLine)); end; Result := Encoded.Size; end; function GetEncodeBASE64String(FileName: string; OutputStream: TStringStream): Integer; var m_bStream:TMemoryStream; m_eStream:TMemoryStream; begin // Result:=S_False; m_bStream:=TMemoryStream.Create; m_eStream:=TMemoryStream.Create; try m_bStream.LoadFromFile(FileName); EncodeBASE64(m_eStream,m_bStream); m_eStream.SaveToStream(OutputStream); Result:=S_Ok; m_bStream.Free; m_eStream.Free; except Result:=s_False; m_bStream.Free; m_eStream.Free; end; end; end.
uses
Classes, SysUtils;
const
_Code64: String[64]=('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/');
type
CharSet = Set of Char; function GetEncodeBASE64String(FileName: string;OutputStream: TStringStream):Integer;
function EncodeBASE64(Encoded, Decoded: TMemoryStream): Integer;
function DecodeBase64(const S, Alphabet: String; const PadSet: CharSet): String;
function StrToBase64(const str: string): string;
function Base64ToStr(const Base64: string): string;
implementation function StrToBase64(const Str: string): string;
var
I, J, K, Len, Len1: Integer;
B3: array[0..2] of Byte;
B4: array[0..3] of Byte;
begin
if Str = '' then
begin
Result := '';
Exit;
end;
Len := Length(Str);
Len1 := ((Len + 2) div 3) shl 2;
SetString(Result, nil, Len1);
I := 1;
J := 1;
while I <= Len do
begin
for K := 0 to 2 do
if K + I > Len then B3[K] := 0
else B3[K] := Ord(Str[K + I]);
B4[0] := B3[0] shr 2;
B4[1] := ((B3[0] shl 4) or (B3[1] shr 4)) and 63;
B4[2] := ((B3[1] shl 2) or (B3[2] shr 6)) and 63;
B4[3] := B3[2] and 63;
for K := 0 to 3 do
begin
case B4[K] of
0..25: Result[J] := Chr(B4[K] + 65); // 'A'..'Z'
26..51: Result[J] := Chr(B4[K] + 71); // 'a'..'z'(B4[K]-26+97)
62: Result[J] := '+';
63: Result[J] := '/';
else Result[J] := Chr(B4[K] - 4); // '0'..'9'(B4[K]-52+48)
end;
// Result[J] := Base64_Chars[B4[K] + 1];
Inc(J);
end;
Inc(I, 3);
end;
K := 3 - Len mod 3 - 1;
if K <> 2 then
for I := Len1 - K to Len1 do
Result[I] := '=';
end; function Base64ToStr(const Base64: string): string;
var
I, J, K, Len, Len1: Integer;
B4: array[0..3] of Byte;
begin
if Base64 = '' then
begin
Result := '';
Exit;
end;
Len := Length(Base64);
if Len and 3 <> 0 then
raise Exception.Create('Invalid Base64 length');
Len1 := (Len shr 2) * 3;
SetString(Result, nil, Len1);
I := 1;
J := 1;
while I <= Len do
begin
for K := 0 to 3 do
begin
case Base64[I] of
'A'..'Z': B4[K] := Ord(Base64[I]) - 65;
'a'..'z': B4[K] := Ord(Base64[I]) - 71;
'0'..'9': B4[K] := Ord(Base64[I]) + 4;
'+': B4[K] := 62;
'/': B4[K] := 63;
'=': B4[K] := 0;
else raise Exception.CreateFmt('#%d: Invalid char in Base64', [Ord(Base64[I])]);
end;
Inc(I);
end;
Result[J] := Chr((B4[0] shl 2) or (B4[1] shr 4));
Result[J + 1] := Chr((B4[1] shl 4) or (B4[2] shr 2));
Result[J + 2] := Chr((B4[2] shl 6) or B4[3]);
Inc(J, 3);
end; I := Pos('=', Base64);
if I <> 0 then
begin
I := Len - I + 1;
Delete(Result, Len1 - I + 1, I);
end; end;
function DecodeBase64(const S, Alphabet: String;
const PadSet: CharSet): String;
var F, L, M, P : Integer;
B, OutPos : Byte;
OutB : Array[1..3] of Byte;
Lookup : Array[Char] of Byte;
R : PChar;
begin
Assert(Length(Alphabet) = 64, 'Alphabet must contain 64 characters');
L := Length(S);
P := 0;
if PadSet <> [] then
While (L - P > 0) and (S[L - P] in PadSet) do
Inc(P);
M := L - P;
if M = 0 then
begin
Result := '';
exit;
end;
SetLength(Result, (M * 3) div 4);
FillChar(Lookup, Sizeof(Lookup), #0);
For F := 0 to 63 do
Lookup[Alphabet[F + 1]] := Byte(F);
R := Pointer(Result);
OutPos := 0;
For F := 1 to L - P do
begin
B := Lookup[S[F]];
Case OutPos of
0 : OutB[1] := B shl 2;
1 : begin
OutB[1] := OutB[1] or (B shr 4);
R^ := Char(OutB[1]);
Inc(R);
OutB[2] := (B shl 4) and $FF;
end;
2 : begin
OutB[2] := OutB[2] or (B shr 2);
R^ := Char(OutB[2]);
Inc(R);
OutB[3] := (B shl 6) and $FF;
end;
3 : begin
OutB[3] := OutB[3] or B;
R^ := Char(OutB[3]);
Inc(R);
end;
end;
OutPos := (OutPos + 1) mod 4;
end;
if (OutPos > 0) and (P = 0) then // incomplete encoding, add the partial byte if not 0
if OutB[OutPos] <> 0 then
Result := Result + Char(OutB[OutPos]);
end; function EncodeBASE64(Encoded, Decoded: TMemoryStream): Integer;
var
I: LongInt;
B: array[0..2279] of Byte;
J, K, L, M, Quads: Integer;
Stream: string[76];
EncLine: String;
begin
Encoded.Clear;
Stream := '';
Quads := 0;
{为提高效率,每2280字节流为一组进行编码}
J := Decoded.Size div 2280;
Decoded.Position := 0;
{对前J*2280个字节流进行编码}
for I := 1 to J do
begin
Decoded.Read(B, 2280);
for M := 0 to 39 do
begin
for K := 0 to 18 do
begin
L:= 57*M + 3*K;
Stream[Quads+1] := _Code64[(B[L] div 4)+1];
Stream[Quads+2] := _Code64[(B[L] mod 4)*16 + (B[L+1] div 16)+1];
Stream[Quads+3] := _Code64[(B[L+1] mod 16)*4 + (B[L+2] div 64)+1];
Stream[Quads+4] := _Code64[B[L+2] mod 64+1];
Inc(Quads, 4);
if Quads = 76 then
begin
Stream[0] := #76;
EncLine := Stream+#13#10;
Encoded.Write(EncLine[1], Length(EncLine));
Quads := 0;
end;
end;
end;
end;
{对以2280为模的余数字节流进行编码}
J := (Decoded.Size mod 2280) div 3;
for I := 1 to J do
begin
Decoded.Read(B, 3);
Stream[Quads+1] := _Code64[(B[0] div 4)+1];
Stream[Quads+2] := _Code64[(B[0] mod 4)*16 + (B[1] div 16)+1];
Stream[Quads+3] := _Code64[(B[1] mod 16)*4 + (B[2] div 64)+1];
Stream[Quads+4] := _Code64[B[2] mod 64+1];
Inc(Quads, 4);
{每行76个字符}
if Quads = 76 then
begin
Stream[0] := #76;
EncLine := Stream+#13#10;
Encoded.Write(EncLine[1], Length(EncLine));
Quads := 0;
end;
end;
{“=”补位}
if (Decoded.Size mod 3) = 2 then
begin
Decoded.Read(B, 2);
Stream[Quads+1] := _Code64[(B[0] div 4)+1];
Stream[Quads+2] := _Code64[(B[0] mod 4)*16 + (B[1] div 16)+1];
Stream[Quads+3] := _Code64[(B[1] mod 16)*4 + 1];
Stream[Quads+4] := '=';
Inc(Quads, 4);
end;
if (Decoded.Size mod 3) = 1 then
begin
Decoded.Read(B, 1);
Stream[Quads+1] := _Code64[(B[0] div 4)+1];
Stream[Quads+2] := _Code64[(B[0] mod 4)*16 + 1];
Stream[Quads+3] := '=';
Stream[Quads+4] := '=';
Inc(Quads, 4);
end;
Stream[0] := Chr(Quads);
if Quads > 0 then
begin
EncLine := Stream+#13#10;
Encoded.Write(EncLine[1], Length(EncLine));
end;
Result := Encoded.Size;
end; function GetEncodeBASE64String(FileName: string;
OutputStream: TStringStream): Integer;
var
m_bStream:TMemoryStream;
m_eStream:TMemoryStream;
begin
// Result:=S_False;
m_bStream:=TMemoryStream.Create;
m_eStream:=TMemoryStream.Create;
try
m_bStream.LoadFromFile(FileName);
EncodeBASE64(m_eStream,m_bStream);
m_eStream.SaveToStream(OutputStream);
Result:=S_Ok;
m_bStream.Free;
m_eStream.Free;
except
Result:=s_False;
m_bStream.Free;
m_eStream.Free;
end;
end;
end.
base64也是一种编码方式,只是体积会大1/3左右
那位大哥能说下思路呢,俺直接放在Image中怎么进行编码呢?
1、把一个bmp图片加载到工具程序,再循环用一个函数生成的值去异或每个点的颜色,保存在一个bmp文件
2、把新bmp文件放在程序里,运行时先循环用一个函数生成的值去异或每个点的颜色再显示
这样,别人是可以从exe里取得新bmp文件,但是可以保证他是看不到内容的