假设Edit1为用户名, Edit2为密码, Edit3为部门
type
Tbm= record
Usename: String[8]; //用户名
Pass: String[10]; // 密码
Equity: ShortString; // 部门
end;
const
C1Key = 12674; // C1 = 52845;
C2Key = 35891; // C2 = 22719;
var
bm: Tbm;
FileHandle: Integer;
function DecryptStr(const S: string; Key: Word): string;
var // 解密
I : Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result := Char(Byte(S) xor (Key shr 8));
Key := (Byte(S) + Key) * C1Key + C2Key;
end;
end;
function EncryptStr(const S: string; Key: Word): string;
var // 加密
I : Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result := Char(Byte(S) xor (Key shr 8));
Key := (Byte(Result) + Key) * C1Key + C2Key;
end;
end;
procedure TForm1.SaveData; // 保存到文件
begin
FileHandle:=FileCreate('./Data.dat');
with bm do begin
Usename:=Edit1.Text ;
Pass:=EncryptStr(edit2.text,123);
Equity:=edit3.text;
end;
FileWrite(FileHandle,bm,Sizeof(bm));
FileClose(fileHandle);
end;
function TForm1.LoadData ; // 从文件载入
begin
FileHandle:=FileOpen('./Data.dat',fmOpenread or fmShareDenyNone);
FileRead(FileHandle,bm,Sizeof(bm));
with Bm do begin
Edit1.Text:=Usename ;
edit2.Text:=DecryptStr(Pass,123);
edit3.Text:=Equity;
end;
fileclose(fileHandle);
end;