我找到的,初步测试解决我的问题,但是算法是依据什么还没看懂[
]
拿出来共享,也希望谁能看出点门道点拨一下
谢谢~
function TForm1.EncrypKey(Src: string;
Key: string): string;
var
KeyLen: Integer;
KeyPos: Integer;
offset: Integer;
dest: string;
SrcPos: Integer;
SrcAsc: Integer;
Range: Integer;
begin
KeyLen := Length(Key);
if KeyLen = 0 then
key := 'Think Space';
KeyPos := 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 TForm1.UncrypKey(Src: string;
Key: string): string;
var
KeyLen: Integer;
KeyPos: Integer;
offset: Integer;
dest: string;
SrcPos: Integer;
SrcAsc: Integer;
TmpSrcAsc: Integer;
begin
KeyLen := Length(Key);
if KeyLen = 0 then
key := 'Think Space';
KeyPos := 0;
offset := StrToInt('$' + copy(src, 1, 2));
SrcPos := 3;
repeat
try
SrcAsc := StrToInt('$' + copy(src, SrcPos, 2));
except
end;
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;