• 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'); 
     
     
     
  • 相关阅读:
    linux 系统下如何进行用户之间的切换
    Linux下如何切换用户
    判断php的运行模式
    一起谈.NET技术,Application、Session、Cookie、ViewState、Cache、Hidden的区别 狼人:
    一起谈.NET技术,HTTP协议及POST与GET操作差异,C#中如何使用POST、GET等 狼人:
    一起谈.NET技术,敏捷开发的26条至理名言 狼人:
    一起谈.NET技术,重温数据库访问——故事篇 狼人:
    一起谈.NET技术,WPF 基础到企业应用系列3——WPF开发漫谈 狼人:
    一起谈.NET技术,使命必达:深入剖析WCF的可靠会话 狼人:
    一起谈.NET技术,.NET中通过代理实现面向方面编程(AOP) 狼人:
  • 原文地址:https://www.cnblogs.com/jijm123/p/11048401.html
Copyright © 2020-2023  润新知