把数据加密后再通过套接字函数发送出去   接收方收到后再解密
所谓加密就是 对数据隔位取反 再把数据调整位置(第一位和第二位对调,第三和第四 等等)。但是我刚学delphi  不是怎么把字符串数据转为二进制  请高手给出加密解密代码

解决方案 »

  1.   

    你出的题难呗,会ASM的都不在。字符串好说:var
    s:string;
    buf:pchar;
    buf1:array of byte;
    begin
     s:='hihisdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdf';
     getmem(buf,length(s));
     setlength(buf1,length(s));
     hextobin(pchar(s),buf,length(s));
     copymemory(@buf1[0],buf,length(s));
     showmessage(inttostr(buf1[0]));
     freemem(buf);
    end;取反
    var
    a,b:byte;
    begin
    a:=$f0;
    b:=not a;
    showmessage(inttostr(b));隔位取反和移位的要求比较难以实现,等高手来吧。
      

  2.   

    procedure Encode(var buf; size: Integer);
    var
      T: Byte;
      P1, P2: PByte;
    begin
      P1 := PByte(@Buf);
      P2 := PByte(PChar(@Buf) + 1);
      while Size > 0 do
      begin
        T := P2^;
        P2^ := not P1^;
        P1^ := not T;
        Inc(P1, 2);
        Inc(P2, 2);
        Dec(Size, 2);
      end;
    end;procedure Decode(var Buf; Size: Integer);
    begin
      Encode(Buf, Size);
    end;
      

  3.   

    楼上的  我是初学者难道delphi自带了这种函数??  函数名是什么?? 在哪个pas里定义的??   要求加密过程利用密钥的    而且可加密特殊字符和汉字
      

  4.   

    a是加密的数据,如果2,4,6,8取反 b是加密后的
    b:= a or ((not a)and 85)
      

  5.   

    上面的错了,这个a是加密的数据,如果2,4,6,8取反 b是加密后的
    b:= (a and 170) or ((not a)and 85)
    CHR也是字节值,所以有ORD()可以转换起ASCII值
    //==================================================procedure TForm1.Button1Click(Sender: TObject);
    var
    a,b:integer;
    begin
      a:=100;
      b:=(a and 170) or ((not a)and 85) ;
      edit1.Text :=inttostr(b);
    end;
      

  6.   

    function encode(a : byte):byte
    var b : byte;
    begin
      b := a xor 85; //隔位取反
      Result := (b shl 1) and 170 or (b shr 1) and 85;  //邻位对调
    end;