zoukankan      html  css  js  c++  java
  • delphi 字符串16进制转换后加密

    function GetStreamString( stream:TStream ):string;   //函数不需要声明和应用
    var
    strStream:TStringStream;
    begin
    strStream:= TStringStream.Create( ' ' );
    try
    strStream.CopyFrom( stream, stream.Size );
    Result:=strStream.DataString;
    finally
    strStream.Free;
    end;
    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;  
    
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
    t : TFileStream;
    s : String;
    i : Int64;
    begin
    t := TFileStream.Create( 'c:1.txt ', fmOpenRead);
    try
    i := t.Size;
    SetLength(s, i);
    t.ReadBuffer(s[1], i);
    ShowMessage (s);
    finally
    t.Free;
    end;
    end;
    
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
         //EncryStrHex('1','2');
         //delphi 对字符串进行加密实现,字符串转16进制,然后再加密
            Edit1.Text:=DecryptStr(Edit1.Text,'delphi');
    end;
    
    procedure TForm1.Button3Click(Sender: TObject);
    begin
         Edit1.Text:=EncryptStr('Hello','delph2i');
         //hello为加密的明文,delphi密钥,密钥不同就无法解密
    end;
    

      

  • 相关阅读:
    Linux之Permission denied没有权限
    soapUI的简单使用(webservice接口功能测试)
    jmeter学习(二),如何安装jmeter?
    loadrunner检查点设置失败,日志中SaveCount无法被正常统计出来
    loadrunner破解出现“license security violation,Operation is not allowed”的错误提示
    安装LoadRunner11报缺少vc2005_sp1_with_atl_fix_redist的错误
    IOS测试,打不开要测试的APP怎么办?设置信任
    Jmeter的好搭档Badboy的安装与简单使用
    映射网络驱动器会自动断开的解决方法
    oracle中如何修改用户名和密码
  • 原文地址:https://www.cnblogs.com/fuhuo/p/11158418.html
Copyright © 2011-2022 走看看