//加密字符串
function EnCryptStr(aStr: string; aKey: string): string;
var
ReadBuf : TBlock;
WriteBuf: TBlock;
Key : TBlock;
Count : Integer;
Offset : Integer;
I : Integer;
S : string;
begin
result := '';
Key := StrToKey(aKey);
Des_Init(Key, True);
Offset := 1;
Count := Length(aStr);
repeat
S := Copy(aStr, Offset, 8);
FillChar(ReadBuf, 8, 0);
Move(S[1], ReadBuf, Length(S));
Des_Code(ReadBuf, WriteBuf);
for I := 0 to 7 do
begin
result := result + IntToHex(WriteBuf[I], 2);
end;
Offset := Offset + 8;
until Offset > ((Count+7) div 8) * 8;
end;
//解密字符串
function DeCryptStr(aStr: string; aKey: string): string;
var
ReadBuf,
WriteBuf : TBlock;
Key : TBlock;
Offset : Integer;
Count : Integer;
I : Integer;
S : string;
begin
try
Key := StrToKey(aKey);
Des_Init(Key, False);
S := '';
I := 1;
repeat
S := S + Chr(StrToInt('$'+Copy(aStr, I, 2)));
Inc(I, 2);
until I > Length(aStr);
Offset := 1;
Count := Length(S);
while Offset < ((Count+7) div 8 * 8) do
begin
FillChar(ReadBuf, 8, 0);
Move(S[Offset], ReadBuf, 8);
Des_Code(ReadBuf, WriteBuf);
for I := 0 to 7 do
begin
result := result + Chr(WriteBuf[I]);
end;
Offset := Offset + 8;
end;
result := StrPas(PChar(result));
except
result := '';
end;
end;