delphi字符串加密解密


//加密函数
Function EncrypKey(Src: String; Key: String): string;
var
idx: integer;
KeyLen: integer;
KeyPos: integer;
offset: integer;
dest: string;
SrcPos: integer;
SrcAsc: integer;
TmpSrcAsc: integer;
Range: integer;

begin
KeyLen := Length(Key);
if KeyLen = 0 then
Key := 'Think Space';
KeyPos := 0;
SrcPos := 0;
SrcAsc := 0;
Range := 256;

Randomize;
offset := Random(Range);
dest := format('%1.2x', [offset]);
for SrcPos := 1 to Length(Src) do
begin
SrcAsc := (Ord(Src[SrcPos]) + offset) MOD 255;
if KeyPos < KeyLen then
KeyPos := KeyPos + 1
else
KeyPos := 1;
SrcAsc := SrcAsc xor ord(Key[KeyPos]);
dest := dest + format('%1.2x', [SrcAsc]);
offset := SrcAsc;
end;
Result := dest;
end;

// 解密函数
Function UncrypKey(Src: String; Key: String): string;
var
idx: integer;
KeyLen: integer;
KeyPos: integer;
offset: integer;
dest: string;
SrcPos: integer;
SrcAsc: integer;
TmpSrcAsc: integer;
Range: integer;

begin
KeyLen := Length(Key);
if KeyLen = 0 then
Key := 'Think Space';
KeyPos := 0;
SrcPos := 0;
SrcAsc := 0;
Range := 256;
offset := StrToInt('$' + copy(Src, 1, 2));
SrcPos := 3;
repeat
SrcAsc := StrToInt('$' + copy(Src, SrcPos, 2));
if KeyPos < KeyLen Then
KeyPos := KeyPos + 1
else
KeyPos := 1;
TmpSrcAsc := SrcAsc xor ord(Key[KeyPos]);
if TmpSrcAsc <= offset then
TmpSrcAsc := 255 + TmpSrcAsc - offset
else
TmpSrcAsc := TmpSrcAsc - offset;
dest := dest + chr(TmpSrcAsc);
offset := SrcAsc;
SrcPos := SrcPos + 2;
until SrcPos >= Length(Src);
Result := dest;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text := EncrypKey(Edit1.Text, stringkey);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Edit3.Text := UncrypKey(Edit2.Text, stringkey)

end;

procedure TForm1.FormShow(Sender: TObject);
begin
stringkey := '11010110';
end;

end.
联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678