DELPHI 암호화 문자열(이중 또는 연산 암호화)

            :

function myStrToHex(s:string):string; //   16     
var  
  TmpStr:string;  
  i:integer;  
begin  
  TmpStr:='';  
  for i:=1 to Length(s)do  
    TmpStr:=TmpStr+IntToHex(ord(s[i]),2);  
  Result:=TmpStr;  
end;  
  
function myHexToStr(S:string):string; //16        
var  
  HexS,TmpStr:string;  
  i:Integer;  
  a:Byte;  
begin  
  HexS:=s;  
  if Length(HexS) mod 2=1 then  
    HexS:=HexS+'0';  
  TmpStr:='';  
  for i:=1 to(Length(HexS)div 2)do  
  begin  
    a:=StrToInt('$'+HexS[2*i-1]+HexS[2*i]);  
    TmpStr:=TmpStr+Chr(a);  
  end;  
  Result:=TmpStr;  
end;  
function myStrToHex(s:string):string; //   16  
var
  TmpStr:string;
  i:integer;
begin
  TmpStr:='';
  for i:=1 to Length(s)do
    TmpStr:=TmpStr+IntToHex(ord(s[i]),2);
  Result:=TmpStr;
end;

function myHexToStr(S:string):string; //16     
var
  HexS,TmpStr:string;
  i:Integer;
  a:Byte;
begin
  HexS:=s;
  if Length(HexS) mod 2=1 then
    HexS:=HexS+'0';
  TmpStr:='';
  for i:=1 to(Length(HexS)div 2)do
  begin
    a:=StrToInt('$'+HexS[2*i-1]+HexS[2*i]);
    TmpStr:=TmpStr+Chr(a);
  end;
  Result:=TmpStr;
end; 

 

      、     :

{         ,s    、     ;sKey   }  
function EncryptStr(const s:string;sKey:string):string; //     
var  
  i,j:Integer;  
  HexS,HexsKey,MidS,TmpStr:string;  
  a,b,c:byte;  
begin  
  HexS:=myStrToHex(s);  
  HexsKey:=myStrToHex(sKey);  
  MidS:=HexS;  
  for i:=1 to(Length(HexsKey)div 2)do  
  begin  
    if i<>1 then MidS:=TmpStr;  
    TmpStr:=''; //     TmpStr,        
    for j:=1 to (Length(MidS)div 2)do  
    begin  
      a:=StrToInt('$'+midS[2*j-1]+midS[2*j]);  
      b:=StrToInt('$'+HexsKey[2*i-1]+HexsKey[2*i]);  
      c:=a xor b;  
      tmpstr:=TmpStr+myStrToHex(Chr(c));  
    end;  
  end;  
  Result:=TmpStr;  
end;  
  
function DecryptStr(const s:string;sKey:string):string; //     
var  
  i,j:Integer;  
  HexS,HexsKey,MidS,TmpStr:string;  
  a,b,c:Byte;  
begin  
  HexS:=s;  
  if Length(HexS)mod 2=1 then //           
  begin  
    ShowMessage('    !');  
    Exit;  
  end;  
  HexsKey:=myStrToHex(sKey);  
  TmpStr:=HexS;  
  MidS:=HexS;  
  for i:=(length(HexsKey) div 2) downto 1 do  
  begin  
    if i<>(length(HexsKey) div 2)then  
      MidS:=TmpStr;  
    TmpStr:=''; //     TmpStr,        
    for j:=1 to(length(MidS) div 2)do  
    begin  
      a:=StrToInt('$'+MidS[2*j-1]+MidS[2*j]);  
      b:=StrToInt('$'+HexsKey[2*i-1]+HexsKey[2*i]);  
      c:=a xor b;  
      TmpStr := TmpStr+myStrToHex(Chr(c));  
    end;  
  end;  
    Result:=myHexToStr(TmpStr);  
end;  
{         ,s    、     ;sKey   }
function EncryptStr(const s:string;sKey:string):string; //  
var
  i,j:Integer;
  HexS,HexsKey,MidS,TmpStr:string;
  a,b,c:byte;
begin
  HexS:=myStrToHex(s);
  HexsKey:=myStrToHex(sKey);
  MidS:=HexS;
  for i:=1 to(Length(HexsKey)div 2)do
  begin
    if i<>1 then MidS:=TmpStr;
    TmpStr:=''; //     TmpStr,     
    for j:=1 to (Length(MidS)div 2)do
    begin
      a:=StrToInt('$'+midS[2*j-1]+midS[2*j]);
      b:=StrToInt('$'+HexsKey[2*i-1]+HexsKey[2*i]);
      c:=a xor b;
      tmpstr:=TmpStr+myStrToHex(Chr(c));
    end;
  end;
  Result:=TmpStr;
end;

function DecryptStr(const s:string;sKey:string):string; //  
var
  i,j:Integer;
  HexS,HexsKey,MidS,TmpStr:string;
  a,b,c:Byte;
begin
  HexS:=s;
  if Length(HexS)mod 2=1 then //        
  begin
    ShowMessage('    !');
    Exit;
  end;
  HexsKey:=myStrToHex(sKey);
  TmpStr:=HexS;
  MidS:=HexS;
  for i:=(length(HexsKey) div 2) downto 1 do
  begin
    if i<>(length(HexsKey) div 2)then
      MidS:=TmpStr;
    TmpStr:=''; //     TmpStr,     
    for j:=1 to(length(MidS) div 2)do
    begin
      a:=StrToInt('$'+MidS[2*j-1]+MidS[2*j]);
      b:=StrToInt('$'+HexsKey[2*i-1]+HexsKey[2*i]);
      c:=a xor b;
      TmpStr := TmpStr+myStrToHex(Chr(c));
    end;
  end;
    Result:=myHexToStr(TmpStr);
end; 

 

         ,  EncryptStr DecryptStr    、  

 

 :

[delphi] view plaincopyprint?Edit1.Text:=EncryptStr('Hello','delphi');  
  
////////////////////////////////////////   
  
Edit1.Text:=DecryptStr(Edit1.Text,'delphi');  
Edit1.Text:=EncryptStr('Hello','delphi');

////////////////////////////////////////

Edit1.Text:=DecryptStr(Edit1.Text,'delphi'); 

 


좋은 웹페이지 즐겨찾기