L
lxftwo
Unregistered / Unconfirmed
GUEST, unregistred user!
我有这些代码 ,但是每次解密时,都会少解出第一个字符??
代码:
function EncryptString(Source, Key: pchar): pchar;
//对字符串加密(Source:源 Key:密匙)
var KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
Range :Integer;
begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='topsoft';
KeyPos:=0;
Range:=256;
Randomize;
offset:=Random(Range);
dest:=format('%1.2x',[offset]);
for SrcPos := 1 to Length(Source) do
begin
SrcAsc:=(Ord(Source[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:=pchar(Dest);
end;
function UnEncryptString (Source, Key: pchar)char;
//对字符串解密(Src:源 Key:密匙)
var KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='topsoft';
KeyPos:=0;
offset:=StrToInt('$'+ copy(Source,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ copy(Source,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(Source);
Result:=pchar(Dest);
end;
代码:
function EncryptString(Source, Key: pchar): pchar;
//对字符串加密(Source:源 Key:密匙)
var KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
Range :Integer;
begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='topsoft';
KeyPos:=0;
Range:=256;
Randomize;
offset:=Random(Range);
dest:=format('%1.2x',[offset]);
for SrcPos := 1 to Length(Source) do
begin
SrcAsc:=(Ord(Source[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:=pchar(Dest);
end;
function UnEncryptString (Source, Key: pchar)char;
//对字符串解密(Src:源 Key:密匙)
var KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='topsoft';
KeyPos:=0;
offset:=StrToInt('$'+ copy(Source,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ copy(Source,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(Source);
Result:=pchar(Dest);
end;