简单写一个:
type
SByte = Array [0..255] of byte;
。。。。。。。
const
C1 = 52845;
C2 = 22719;
Key = 2008;
procedure EncryptFile(fName: string);
procedure Encrypt(S: SByte; var t: SByte; Key: Word);
var
I: byte;
begin
t[0] := S[0];
for I := 0 to 255 do begin
t := S xor (Key shr 8);
Key := (t + Key) * C1 + C2;
end;
end;
var
F: file;
Num: DWord;
temp1, temp2: SByte;
begin
AssignFile(F, fname);
if FileExists(fname) then begin
Reset(F,1);
Seek(F, 0);
BlockRead(F, temp1, sizeof(temp1), Num);
Encrypt(temp1,temp2, Key);
Seek(F, 0);
BlockWrite(F, temp2, sizeof(temp2), Num);
CloseFile(F);
end;
end;
procedure DeCryptFile(fName: string);
procedure Decrypt(S: SByte; var t: SByte; Key: Word);
var
I: byte;
begin
t[0] := S[0];
for I := 0 to 255 do begin
t := S xor (Key shr 8);
Key := (S + Key) * C1 + C2;
end;
end;
var
F: file;
Num: DWord;
temp1, temp2: SByte;
begin
AssignFile(F, fname);
if FileExists(fname) then begin
Reset(F,1);
Seek(F, 0);
BlockRead(F, temp1, sizeof(temp1), Num);
Decrypt(temp1,temp2, Key);
Seek(F, 0);
BlockWrite(F, temp2, sizeof(temp2), Num);
CloseFile(F);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin //加密
if OpenDialog1.Execute then
EncryptFile(OpenDialog1.FileName);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin //解密
if OpenDialog1.Execute then
DecryptFile(OpenDialog1.FileName);
end;