# delphi7的加密算法与 delphi XE+加解密算法

delphi7的加密算法与 delphi XE+加解密算法

delphi7加解密算法：

Function JiaMi(Src:String; Key:String):String;
var
KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :String;
SrcPos :Integer;
SrcAsc :Integer;
Range :Integer;
begin
KeyLen:=Length(Key);
KeyPos:=0;
Range:=1;
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 JieMi (Src:String; Key:String):String;
var
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:='starlin';
KeyPos:=0;
SrcPos:=0;
SrcAsc:=0;
Range:=1;
offset:=StrToInt('$'+ copy(src,1,2)); SrcPos:=3; repeat try SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
except
SrcAsc:=StrToInt('$00'); 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; Result:=Copy(Result,1,Length(Result)-1); end; end.  Delphi XE+版本： Function JiaMi(Src:String; Key:String):String; // Src 支持汉字，字母，数字，符号 // Key 不支持汉字；适合字母与数字 var KeyLen :Integer; KeyPos :Integer; offset :Integer; dest :String; SrcPos :Integer; SrcAsc :Integer; Range :Integer; IntTemp:integer; SrcAnsi:PAnsiChar; begin KeyLen:=Length(Key); KeyPos:=0; Range:=1; Randomize; offset:=Random(Range); dest:=format('%1.2x',[offset]); SrcAnsi:=PAnsiChar(AnsiString(Src)); IntTemp:= Length(SrcAnsi); for SrcPos := 1 to IntTemp do begin SrcAsc:=(Ord(SrcAnsi[SrcPos-1]) + 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 JieMi (Src:String; Key:String):String; // Src 支持汉字，字母，数字，符号 // Key 不支持汉字；适合字母与数字 var KeyLen :Integer; KeyPos :Integer; offset :Integer; dest :String; SrcPos :Integer; SrcAsc :Integer; TmpSrcAsc :Integer; Range :Integer; SrcAnsi:PAnsiChar; IntTemp,i:Integer; ByteArray: array of Byte; begin KeyLen:=Length(Key); if KeyLen = 0 then key:='starlin'; KeyPos:=0; SrcPos:=0; SrcAsc:=0; Range:=1; offset:=StrToInt('$'+ copy(src,1,2));
SrcPos:=3;
IntTemp:=Length(Src) div 2;
setlength(ByteArray,IntTemp);
i:=0;
repeat
try
SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2)); except SrcAsc:=StrToInt('$00');
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;
ByteArray[i]:=TmpSrcAsc;
i:=i+1;
offset:=srcAsc;
SrcPos:=SrcPos + 2;
until SrcPos >= Length(Src);
GetMem(SrcAnsi,IntTemp+1);
CopyMemory(@SrcAnsi[0],@ByteArray[0],IntTemp);
dest:=string(strpas(SrcAnsi));
Result:=Dest;
end;


str1 :=jiemi(jiami(‘学习ABC123’,‘uabd123’),‘uabd123’);
//返回值：学习ABC123

10-20
06-30
04-16 3122
12-07 1389
12-28 1万+
07-03 3921
11-16 29
06-02 2589
02-27 5079
03-30 3907
01-16 54
09-27 130