program Crypt; uses WinCRT; const C1 = 52845; C2 = 22719; function Encrypt(const S: String; Key: Word): String; var I: byte; begin Result[0] := S[0]; for I := 1 to Length(S) do begin Result[I] := char(byte(S[I]) xor (Key shr 8)); Key := (byte(Result[I]) + Key) * C1 + C2; end; end;function Decrypt(const S: String; Key: Word): String; var I: byte; begin Result[0] := S[0]; for I := 1 to Length(S) do begin Result[I] := char(byte(S[I]) xor (Key shr 8)); Key := (byte(S[I]) + Key) * C1 + C2; end; end;var S: string; begin Write('>'); ReadLn(S); S := Encrypt(S,12345); WriteLn(S); S := Decrypt(S,12345); WriteLn(S); end. ////////////////////////////////////////////////////// unit Unit2;interfaceConst Allchar: string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789';procedure Encrypt( var ss: string );implementationprocedure Encrypt( var ss: string ); var l, lac, // string length sp, // ss char pointer cp: integer; // allchar pointer begin l := Length(ss); lac := Length( Allchar ); sp := 1; while sp <= l do begin cp := 1; while (allchar[cp] <> ss[sp]) and ( cp <= lac ) do inc( cp ); { match char and find the encrypted counterpart in the reverse order in position } if cp > lac then ss[sp]:= '*' { Mark illegal char - use only char not in allchar } else begin { Un-re next line will further enhance security... such that same character will appear as different after encrypt }// cp := (( cp + sp*2 ) mod lac) + 1; ss[sp] := allchar[ lac - cp + 1 ]; //first char result in the last end; inc(sp); end; end; end.
伴水的一个函数:(*// 标题:字符串加密;pascal字符表示 说明:应用于文件加密 设计:Zswang 日期:2002-02-19 支持:[email protected] //*)///////Begin Source function StringToDisplay(mString: string): string; var I: Integer; S: string; begin Result := ''; S := ''; for I := 1 to Length(mString) do if mString[I] in [#32..#127] then S := S + mString[I] else begin if S <> '' then begin Result := Result + QuotedStr(S); S := ''; end; Result := Result + Format('#$%x', [Ord(mString[I])]); end; if S <> '' then Result := Result + QuotedStr(S); end; { StringToDisplay }function DisplayToString(mDisplay: string): string; var I: Integer; S: string; B: Boolean; begin Result := ''; B := False; mDisplay := mDisplay; for I := 1 to Length(mDisplay) do if B then case mDisplay[I] of '''': begin if S <> '' then Result := Result + StringReplace(S, '''''', '''', [rfReplaceAll]); if Copy(mDisplay, I + 1, 1) = '''' then Result := Result + ''''; S := ''; B := False; end; else S := S + mDisplay[I]; end else case mDisplay[I] of '#', '''': begin if S <> '' then Result := Result + Chr(StrToIntDef(S, 0)); S := ''; B := mDisplay[I] = ''''; end; '$', '0'..'9', 'a'..'f', 'A'..'F': S := S + mDisplay[I]; end; if (not B) and (S <> '') then Result := Result + Chr(StrToIntDef(S, 0)); end; { DisplayToString }function StringEncrypt(mStr: string; mKey: string): string; var I, J: Integer; begin J := 1; Result := ''; for I := 1 to Length(mStr) do begin Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J])); if J + 1 <= Length(mKey) then Inc(J) else J := 1; end; { 自己加步骤 } end; { StringEncrypt }function StringDecrypt(mStr: string; mKey: string): string; var I, J: Integer; begin J := 1; Result := ''; { 自己加步骤 } for I := 1 to Length(mStr) do begin Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J])); if J + 1 <= Length(mKey) then Inc(J) else J := 1; end; end; { StringDecrypt } ///////End Source///////Begin Demo const cKey = '给你这一把钥匙,只能打开这一把锁';procedure TForm1.Button1Click(Sender: TObject); begin Memo2.Text := StringToDisplay(StringEncrypt(Memo1.Text, cKey)); end;procedure TForm1.Button2Click(Sender: TObject); begin Memo1.Text := StringDecrypt(DisplayToString(Memo2.Text), cKey); end; ///////End Demo
直接加密和解密算法:{*******************************************************} { } { Decrypt } { } { bitwise compare of each characters XOR 27 } { } { Return string which after bitwise compare } { } {*******************************************************} function Decrypt(s: string; Key: Integer = 27): string; var i: Integer; begin Result := s; for i := 1 to Length(s) do Result[i] := Chr(Ord(s[i]) xor Key); end;{*******************************************************} { } { Encrypt } { } { Call again Decrypt to back to origin } { } { Return string which after bitwise compare } { } {*******************************************************} function Encrypt(s: string; Key : Integer =27): string; begin Result := Decrypt(s, Key); end; 够不够快??
to cg1120: 太慢,我加密一个20K的文件要花上5秒多的时间,而且加密后的文件尺寸增大了许多。
大家别笑我啊~~ 虽然加密强度不够高,还是挺实用的,如下:procedure cryptFile(srcFile,dstFile:TFilename); var srcf,dstf:file of byte; buf:byte; begin assign(srcf,srcfile); assign(dstf,dstfile); reset(srcf); rewrite(dstf); while not eof(srcf) do begin read(srcf,buf); buf:=buf+1; //这里可以修改成其他的数值 write(dstf,buf); end; closefile(srcf); closefile(dstf); end;procedure decryptFile(srcFile,dstFile:TFilename); var srcf,dstf:file of byte; buf:byte; begin assign(srcf,srcfile); assign(dstf,dstfile); reset(srcf); rewrite(dstf); while not eof(srcf) do begin read(srcf,buf); buf:=buf-1; //这里可以作相应修改 write(dstf,buf); end; closefile(srcf); closefile(dstf); end;procedure TForm1.Button1Click(Sender: TObject); begin cryptfile('c:\jjj.txt','c:\zzz.dat'); end;procedure TForm1.Button2Click(Sender: TObject); begin decryptFile('c:\zzz.dat','c:\zzz.txt'); end;
uses WinCRT;
const
C1 = 52845;
C2 = 22719;
function Encrypt(const S: String; Key: Word): String;
var
I: byte;
begin
Result[0] := S[0];
for I := 1 to Length(S) do begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(Result[I]) + Key) * C1 + C2;
end;
end;function Decrypt(const S: String; Key: Word): String;
var
I: byte;
begin
Result[0] := S[0];
for I := 1 to Length(S) do begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(S[I]) + Key) * C1 + C2;
end;
end;var
S: string;
begin
Write('>');
ReadLn(S);
S := Encrypt(S,12345);
WriteLn(S);
S := Decrypt(S,12345);
WriteLn(S);
end.
//////////////////////////////////////////////////////
unit Unit2;interfaceConst Allchar: string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789';procedure Encrypt( var ss: string );implementationprocedure Encrypt( var ss: string );
var l, lac, // string length
sp, // ss char pointer
cp: integer; // allchar pointer
begin
l := Length(ss);
lac := Length( Allchar );
sp := 1;
while sp <= l do begin
cp := 1;
while (allchar[cp] <> ss[sp]) and ( cp <= lac ) do inc( cp );
{ match char and find the encrypted counterpart in the reverse
order in position }
if cp > lac then ss[sp]:= '*'
{ Mark illegal char - use only char not in allchar }
else begin
{ Un-re next line will further enhance security...
such that same character will appear as
different after encrypt }// cp := (( cp + sp*2 ) mod lac) + 1; ss[sp] := allchar[ lac - cp + 1 ]; //first char result in the last
end;
inc(sp);
end;
end;
end.
标题:字符串加密;pascal字符表示
说明:应用于文件加密
设计:Zswang
日期:2002-02-19
支持:[email protected]
//*)///////Begin Source
function StringToDisplay(mString: string): string;
var
I: Integer;
S: string;
begin
Result := '';
S := '';
for I := 1 to Length(mString) do
if mString[I] in [#32..#127] then
S := S + mString[I]
else begin
if S <> '' then begin
Result := Result + QuotedStr(S);
S := '';
end;
Result := Result + Format('#$%x', [Ord(mString[I])]);
end;
if S <> '' then Result := Result + QuotedStr(S);
end; { StringToDisplay }function DisplayToString(mDisplay: string): string;
var
I: Integer;
S: string;
B: Boolean;
begin
Result := '';
B := False;
mDisplay := mDisplay;
for I := 1 to Length(mDisplay) do
if B then case mDisplay[I] of
'''': begin
if S <> '' then Result := Result + StringReplace(S, '''''', '''', [rfReplaceAll]);
if Copy(mDisplay, I + 1, 1) = '''' then Result := Result + '''';
S := '';
B := False;
end;
else S := S + mDisplay[I];
end
else case mDisplay[I] of
'#', '''': begin
if S <> '' then Result := Result + Chr(StrToIntDef(S, 0));
S := '';
B := mDisplay[I] = '''';
end;
'$', '0'..'9', 'a'..'f', 'A'..'F': S := S + mDisplay[I];
end;
if (not B) and (S <> '') then Result := Result + Chr(StrToIntDef(S, 0));
end; { DisplayToString }function StringEncrypt(mStr: string; mKey: string): string;
var
I, J: Integer;
begin
J := 1;
Result := '';
for I := 1 to Length(mStr) do begin
Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
if J + 1 <= Length(mKey) then
Inc(J)
else J := 1;
end;
{ 自己加步骤 }
end; { StringEncrypt }function StringDecrypt(mStr: string; mKey: string): string;
var
I, J: Integer;
begin
J := 1;
Result := '';
{ 自己加步骤 }
for I := 1 to Length(mStr) do begin
Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
if J + 1 <= Length(mKey) then
Inc(J)
else J := 1;
end;
end; { StringDecrypt }
///////End Source///////Begin Demo
const
cKey = '给你这一把钥匙,只能打开这一把锁';procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text := StringToDisplay(StringEncrypt(Memo1.Text, cKey));
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := StringDecrypt(DisplayToString(Memo2.Text), cKey);
end;
///////End Demo
{ }
{ Decrypt }
{ }
{ bitwise compare of each characters XOR 27 }
{ }
{ Return string which after bitwise compare }
{ }
{*******************************************************}
function Decrypt(s: string; Key: Integer = 27): string;
var
i: Integer;
begin
Result := s;
for i := 1 to Length(s) do
Result[i] := Chr(Ord(s[i]) xor Key);
end;{*******************************************************}
{ }
{ Encrypt }
{ }
{ Call again Decrypt to back to origin }
{ }
{ Return string which after bitwise compare }
{ }
{*******************************************************}
function Encrypt(s: string; Key : Integer =27): string;
begin
Result := Decrypt(s, Key);
end;
够不够快??
var
srcf,dstf:file of byte;
buf:byte;
begin
assign(srcf,srcfile);
assign(dstf,dstfile);
reset(srcf); rewrite(dstf);
while not eof(srcf) do
begin
read(srcf,buf);
buf:=buf+1; //这里可以修改成其他的数值
write(dstf,buf);
end;
closefile(srcf);
closefile(dstf);
end;procedure decryptFile(srcFile,dstFile:TFilename);
var
srcf,dstf:file of byte;
buf:byte;
begin
assign(srcf,srcfile);
assign(dstf,dstfile);
reset(srcf); rewrite(dstf);
while not eof(srcf) do
begin
read(srcf,buf);
buf:=buf-1; //这里可以作相应修改
write(dstf,buf);
end;
closefile(srcf);
closefile(dstf);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
cryptfile('c:\jjj.txt','c:\zzz.dat');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
decryptFile('c:\zzz.dat','c:\zzz.txt');
end;
s = s xor key;
不错,直接对文件操作,不用转成字符串。