delphi实现动态加密解密功能。

原创
2017/11/25 03:09
阅读数 573
function Byte2Hex2(arrByte:TByteArr):string;
var
  i:integer;
begin
  for i:=0 to length(arrByte)-1 do
  begin
    Result := Result + Copy( IntToHex(arrByte[i],16),15,2)
  end;
end;
function StrToByte(const Value: String): TByteArr;
 var
 I: integer;
  begin
   SetLength(Result, Length(Value));
    for I := 0 to Length(Value) - 1 do
    Result[I] := ord(Value[I + 1]);
  end;
function ByteToString(const Value: TByteArr): String;
 var
 I: integer;
 S : String;
 Letra: char;
 begin
      S := '';
      for I := Length(Value)-1 Downto 0 do
      begin
       letra := Chr(Value[I] );
         S := letra + S;     end;
    Result := S;
 end;
 function DynamicDec(resource:TByteArr;key:string): TByteArr;
 var
 arrbyte,Rndbyte,RndbyteKey,pBytes,pBytes1,cbtemp: TByteArr;
 lenKey,Lenresource,posInt,I,m,n:integer;
 begin
  Lenresource:=length(resource);
 if Lenresource>8 then
 begin

   if key='' then
      key:='QWERTYUIOPLKJHGFFDSAZXCVBNM~!@#$%^&*()_+-=';
    arrbyte:=strToByte(key);
    lenKey:=length(arrbyte);
    if lenKey>0 then
    begin
      //posInt:=0;
      SetLength(pBytes,4);
      SetLength(pBytes1,4);
      for i := 0 to 7 do
      begin
      if i<4 then
       begin
          pBytes[i]:=resource[i];
       end
       else
       begin
          pBytes1[i-4]:=resource[i];
       end;
       end;

      posInt:=PInteger(pBytes)^ xor PInteger(pBytes1)^;
      posInt:=not (posInt) ;
      SetLength(Rndbyte,4);
      SetLength(RndbyteKey,4);

     for I := 4 to 7 do
     begin
      Rndbyte[i-4]:=resource[i];
      RndbyteKey[i-4]:= resource[i] xor posInt;

     end;
     m:=0;
     for I := 0 to lenKey-1 do
     begin
      m:=m+1;
      if m>4 then
         m:=1  ;

      arrbyte[i]:=arrbyte[i] xor Rndbyte[m-1];
      end;
      SetLength(cbtemp,Lenresource-8);
      for I := 8 to Lenresource-1 do
      begin
      m:=m+1;
      if m>4 then
         m:=1  ;

      resource[i]:=resource[i] xor RndbyteKey[m-1]   ;

      for n := 0 to lenKey-1 do
       begin
        resource[i]:=resource[i] xor arrbyte[n]   ;
       end;
       cbtemp[i-8]:=resource[i];

     end;

    end;
 end;
 result:=cbtemp;
end;

 function  DynamicEnc(resource:TByteArr;key:string): TByteArr;
 var
 arrbyte,Rndbyte,temp: TByteArr;
 lenKey,Lenresource,I,m,n,ValueRand:integer;
 FBytes : TByteArr;
 begin
 Randomize;
 if key='' then
      key:='QWERTYUIOPLKJHGFFDSAZXCVBNM~!@#$%^&*()_+-=';
 SetLength(Rndbyte,4);
 SetLength(temp,4);
 arrbyte:=strToByte(key);

 lenKey:=length(arrbyte);
 Lenresource:=length(resource);
 ValueRand:=random(255);

 for i:= 0 to 3 do
   begin
   Rndbyte[i]:=random(255) ;
   temp[i]:=Rndbyte[i]  ;
   end;

 if (lenKey>0) and (lenresource>0) then
  begin
    for I := 0 to 3 do
         Rndbyte[i]:=Rndbyte[i] xor ValueRand  ;

    m:=0;
    for I := 0 to lenKey-1 do
    begin
      m:=m+1;
      if m>4 then
         m:=1;
       arrbyte[i]:=arrbyte[i] xor Rndbyte[m-1];
    end;
    for I := 0 to Lenresource-1 do
    begin
       m:=m+1;

      if m>4 then
         m:=1;
       for n := 0 to lenKey-1 do
       begin
        resource[i]:= (resource[i]) xor arrbyte[n];
       end;
       resource[i]:=  resource[i] xor temp[m-1];
    end;
      ValueRand:= not ValueRand ;
      ValueRand:= ValueRand xor PInteger(Rndbyte)^;
      SetLength(FBytes,4);
      Move(ValueRand, FBytes[0], SizeOf(FBytes));
      resource:=FBytes+Rndbyte+resource;
   end;
   result:=resource;
 end;

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部