MD5CryptoServiceProvider provider1 = new MD5CryptoServiceProvider();
byte[] buffer1 = new UTF8Encoding().GetBytes(this.txtpassword.Text);
byte[] buffer2 = provider1.ComputeHash(buffer1);
modMain.PHASH = Convert.ToBase64String(buffer2);如何把上面的代码用delphi7来实现?网上找的delphi MD5算法都是标准的,跟上面的结果不一致,ToBase64String 的.net中的,delphi中没有。

解决方案 »

  1.   

    DELPHI2005里没有吗?这是。NET类库里的啊。
      

  2.   

    我知道,我只是想用delphi7来做,这个MD5我想明白了,现在就是这个ToBase64String的问题了。
      

  3.   

    const
      B64Table= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';function B64Encode(const S: String): String;
    var
      i: integer;
      InBuf: array[0..2] of byte;
      OutBuf: array[0..3] of char;
    begin
      SetLength(Result,((Length(S)+2) div 3)*4);
      for i:= 1 to ((Length(S)+2) div 3) do
      begin
        if Length(S)< (i*3) then
          Move(S[(i-1)*3+1],InBuf,Length(S)-(i-1)*3)
        else
          Move(S[(i-1)*3+1],InBuf,3);
        OutBuf[0]:= B64Table[((InBuf[0] and $FC) shr 2) + 1];
        OutBuf[1]:= B64Table[(((InBuf[0] and $03) shl 4) or ((InBuf[1] and $F0) shr 4)) + 1];
        OutBuf[2]:= B64Table[(((InBuf[1] and $0F) shl 2) or ((InBuf[2] and $C0) shr 6)) + 1];
        OutBuf[3]:= B64Table[(InBuf[2] and $3F) + 1];
        Move(OutBuf,Result[(i-1)*4+1],4);
      end;
      if (Length(S) mod 3)= 1 then
      begin
        Result[Length(Result)-1]:= '=';
        Result[Length(Result)]:= '=';
      end
      else if (Length(S) mod 3)= 2 then
        Result[Length(Result)]:= '=';
    end;function B64Decode(const S: String): String;
    var
      i: integer;
      InBuf: array[0..3] of byte;
      OutBuf: array[0..2] of byte;
    begin
      if (Length(S) mod 4)<> 0 then
        raise Exception.Create('Base64: Incorrect string format');
      SetLength(Result,((Length(S) div 4)-1)*3);
      for i:= 1 to ((Length(S) div 4)-1) do
      begin
        Move(S[(i-1)*4+1],InBuf,4);
        if (InBuf[0]> 64) and (InBuf[0]< 91) then
          Dec(InBuf[0],65)
        else if (InBuf[0]> 96) and (InBuf[0]< 123) then
          Dec(InBuf[0],71)
        else if (InBuf[0]> 47) and (InBuf[0]< 58) then
          Inc(InBuf[0],4)
        else if InBuf[0]= 43 then
          InBuf[0]:= 62
        else
          InBuf[0]:= 63;
        if (InBuf[1]> 64) and (InBuf[1]< 91) then
          Dec(InBuf[1],65)
        else if (InBuf[1]> 96) and (InBuf[1]< 123) then
          Dec(InBuf[1],71)
        else if (InBuf[1]> 47) and (InBuf[1]< 58) then
          Inc(InBuf[1],4)
        else if InBuf[1]= 43 then
          InBuf[1]:= 62
        else
          InBuf[1]:= 63;
        if (InBuf[2]> 64) and (InBuf[2]< 91) then
          Dec(InBuf[2],65)
        else if (InBuf[2]> 96) and (InBuf[2]< 123) then
          Dec(InBuf[2],71)
        else if (InBuf[2]> 47) and (InBuf[2]< 58) then
          Inc(InBuf[2],4)
        else if InBuf[2]= 43 then
          InBuf[2]:= 62
        else
          InBuf[2]:= 63;
        if (InBuf[3]> 64) and (InBuf[3]< 91) then
          Dec(InBuf[3],65)
        else if (InBuf[3]> 96) and (InBuf[3]< 123) then
          Dec(InBuf[3],71)
        else if (InBuf[3]> 47) and (InBuf[3]< 58) then
          Inc(InBuf[3],4)
        else if InBuf[3]= 43 then
          InBuf[3]:= 62
        else
          InBuf[3]:= 63;
        OutBuf[0]:= (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $03);
        OutBuf[1]:= (InBuf[1] shl 4) or ((InBuf[2] shr 2) and $0F);
        OutBuf[2]:= (InBuf[2] shl 6) or (InBuf[3] and $3F);
        Move(OutBuf,Result[(i-1)*3+1],3);
      end;
      if Length(S)<> 0 then
      begin
        Move(S[Length(S)-3],InBuf,4);
        if InBuf[2]= 61 then
        begin
          if (InBuf[0]> 64) and (InBuf[0]< 91) then
            Dec(InBuf[0],65)
          else if (InBuf[0]> 96) and (InBuf[0]< 123) then
            Dec(InBuf[0],71)
          else if (InBuf[0]> 47) and (InBuf[0]< 58) then
            Inc(InBuf[0],4)
          else if InBuf[0]= 43 then
            InBuf[0]:= 62
          else
            InBuf[0]:= 63;
          if (InBuf[1]> 64) and (InBuf[1]< 91) then
            Dec(InBuf[1],65)
          else if (InBuf[1]> 96) and (InBuf[1]< 123) then
            Dec(InBuf[1],71)
          else if (InBuf[1]> 47) and (InBuf[1]< 58) then
            Inc(InBuf[1],4)
          else if InBuf[1]= 43 then
            InBuf[1]:= 62
          else
            InBuf[1]:= 63;
          OutBuf[0]:= (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $03);
          Result:= Result + char(OutBuf[0]);
        end
        else if InBuf[3]= 61 then
        begin
          if (InBuf[0]> 64) and (InBuf[0]< 91) then
            Dec(InBuf[0],65)
          else if (InBuf[0]> 96) and (InBuf[0]< 123) then
            Dec(InBuf[0],71)
          else if (InBuf[0]> 47) and (InBuf[0]< 58) then
            Inc(InBuf[0],4)
          else if InBuf[0]= 43 then
            InBuf[0]:= 62
          else
            InBuf[0]:= 63;
          if (InBuf[1]> 64) and (InBuf[1]< 91) then
            Dec(InBuf[1],65)
          else if (InBuf[1]> 96) and (InBuf[1]< 123) then
            Dec(InBuf[1],71)
          else if (InBuf[1]> 47) and (InBuf[1]< 58) then
            Inc(InBuf[1],4)
          else if InBuf[1]= 43 then
            InBuf[1]:= 62
          else
            InBuf[1]:= 63;
          if (InBuf[2]> 64) and (InBuf[2]< 91) then
            Dec(InBuf[2],65)
          else if (InBuf[2]> 96) and (InBuf[2]< 123) then
            Dec(InBuf[2],71)
          else if (InBuf[2]> 47) and (InBuf[2]< 58) then
            Inc(InBuf[2],4)
          else if InBuf[2]= 43 then
            InBuf[2]:= 62
          else
            InBuf[2]:= 63;
          OutBuf[0]:= (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $03);
          OutBuf[1]:= (InBuf[1] shl 4) or ((InBuf[2] shr 2) and $0F);
          Result:= Result + char(OutBuf[0]) + char(OutBuf[1]);
        end
        else
        begin
          if (InBuf[0]> 64) and (InBuf[0]< 91) then
            Dec(InBuf[0],65)
          else if (InBuf[0]> 96) and (InBuf[0]< 123) then
            Dec(InBuf[0],71)
          else if (InBuf[0]> 47) and (InBuf[0]< 58) then
            Inc(InBuf[0],4)
          else if InBuf[0]= 43 then
            InBuf[0]:= 62
          else
            InBuf[0]:= 63;
          if (InBuf[1]> 64) and (InBuf[1]< 91) then
            Dec(InBuf[1],65)
          else if (InBuf[1]> 96) and (InBuf[1]< 123) then
            Dec(InBuf[1],71)
          else if (InBuf[1]> 47) and (InBuf[1]< 58) then
            Inc(InBuf[1],4)
          else if InBuf[1]= 43 then
            InBuf[1]:= 62
          else
            InBuf[1]:= 63;
          if (InBuf[2]> 64) and (InBuf[2]< 91) then
            Dec(InBuf[2],65)
          else if (InBuf[2]> 96) and (InBuf[2]< 123) then
            Dec(InBuf[2],71)
          else if (InBuf[2]> 47) and (InBuf[2]< 58) then
            Inc(InBuf[2],4)
          else if InBuf[2]= 43 then
            InBuf[2]:= 62
          else
            InBuf[2]:= 63;
          if (InBuf[3]> 64) and (InBuf[3]< 91) then
            Dec(InBuf[3],65)
          else if (InBuf[3]> 96) and (InBuf[3]< 123) then
            Dec(InBuf[3],71)
          else if (InBuf[3]> 47) and (InBuf[3]< 58) then
            Inc(InBuf[3],4)
          else if InBuf[3]= 43 then
            InBuf[3]:= 62
          else
            InBuf[3]:= 63;
          OutBuf[0]:= (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $03);
          OutBuf[1]:= (InBuf[1] shl 4) or ((InBuf[2] shr 2) and $0F);
          OutBuf[2]:= (InBuf[2] shl 6) or (InBuf[3] and $3F);
          Result:= Result + Char(OutBuf[0]) + Char(OutBuf[1]) + Char(OutBuf[2]);
        end;
      end;
    end;
      

  4.   

    上面的编码我也找来试过了,但是不对啊!编码结果不可能是24位(包括最后的==),例如1的结果是xMpCOKC5I4INzFCab3WEmw==
      

  5.   

    测试过了,完全正确,怎么不可能???先做MD5取到Hash值,再直接做Base64编码,结果完全一样。你都说了自己明白MD5,我当然只给你Base64编码的实现唯一要注意你那份代码Base64编码的是MD5得到的Hash值是Byte数组而不是字符串。
      

  6.   

    高人,请再指点,我再网上找了Md5的delphi实现文件,假定直接可以用M5()来用,你的意思就是
    B64Encode(Md5('1'))='xMpCOKC5I4INzFCab3WEmw==',我得到的结果比这个位数多,c#的MD5得到值是大写的,我转换成大写也不对,恳请帮忙!
      

  7.   

    老大,能把你的测试代码发给我吗?[email protected] ,最近急用,非常感谢!!!
      

  8.   

    你那个MD5得到的是字符串,但要用的是字节序来处理比如'CA' => 0xCA,用0xCA来进行Base64编码,而非字符串'CA'
      

  9.   

    看不懂byte[]是什么意思吗???
      

  10.   

    B64Encode(const S: String)输入的是字符串,MD5处理的结果,该如何处理后调用B64Encode才是正确的结果?不好意思,还是搞不明白!
      

  11.   

    Byte是无符号8位,0-255,你的意思是不是对MD5生成的字符串中每个字符转换成Byte,然后合并成一个数字字符串,再进行Base64编码?