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;
{*******************************************************}
{ }
{ 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;
///////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
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;
procedure 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-remark 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;