如果要做,具体方法是怎样的,用Delphi如何实现?

解决方案 »

  1.   

    主    题: 用Delphi实现MD5算法 
    作       者:    BlueSong  
    等    级: 版主    
    信    誉: 100 
    所属论坛: 开发语言 >> Delphi 
    问题分数: 10 
    发表时间: 2003-04-11 14:03 --------------------------------------------------------------------------------
     
    unit MD5;interface  procedure MD5Code(var InData; var OutData; cByte: Integer);implementationuses
      Windows, SysUtils;const
       Ca = $67452301;
       Cb = $EFCDAB89;
       Cc = $98BADCFE;
       Cd = $10325476;  T: array [0..3,0..15] of Cardinal =
       (($d76aa478,
         $e8c7b756,
         $242070db,
         $c1bdceee,
         $f57c0faf,
         $4787c62a,
         $a8304613,
         $fd469501,
         $698098d8,
         $8b44f7af,
         $ffff5bb1,
         $895cd7be,
         $6b901122,
         $fd987193,
         $a679438e,
         $49b40821),    ($f61e2562,
         $c040b340,
         $265e5a51,
         $e9b6c7aa,
         $d62f105d,
         $02441453,
         $d8a1e681,
         $e7d3fbc8,
         $21e1cde6,
         $c33707d6,
         $f4d50d87,
         $455a14ed,
         $a9e3e905,
         $fcefa3f8,
         $676f02d9,
         $8d2a4c8a),    ($fffa3942,
         $8771f681,
         $6d9d6122,
         $fde5380c,
         $a4beea44,
         $4bdecfa9,
         $f6bb4b60,
         $bebfbc70,
         $289b7ec6,
         $eaa127fa,
         $d4ef3085,
         $04881d05,
         $d9d4d039,
         $e6db99e5,
         $1fa27cf8,
         $c4ac5665),    ($f4292244,
         $432aff97,
         $ab9423a7,
         $fc93a039,
         $655b59c3,
         $8f0ccc92,
         $ffeff47d,
         $85845dd1,
         $6fa87e4f,
         $fe2ce6e0,
         $a3014314,
         $4e0811a1,
         $f7537e82,
         $bd3af235,
         $2ad7d2bb,
         $eb86d391));function ROL (Value: Cardinal; Shift: Byte): Cardinal;
    begin
      Assert(Shift <= 32);
      Result := (Value shl Shift) or (Value shr (32 - Shift));
    end;procedure FF(var a: Cardinal; b,c,d,Mj,s,ti: Cardinal);
      function F(X, Y, Z: Cardinal): Cardinal;
      begin
        Result :=(X and Y) or ((not X) and Z);
      end;
    begin
      a := b +  ROL(a +(F(b,c,d) + Mj + ti), s);
    end;procedure GG(var a: Cardinal; b,c,d,Mj,s,ti: Cardinal);
      function G(X, Y, Z: Cardinal): Cardinal;
      begin
        Result := (X and Z) or (Y and (not Z));
      end;
    begin
      a := b +  ROL(a +(G(b,c,d) + Mj + ti), s);
    end;procedure HH(var a: Cardinal; b,c,d,Mj,s,ti: Cardinal);
      function H(X, Y, Z: Cardinal): Cardinal;
      begin
        Result := X xor Y xor Z;
      end;
    begin
      a := b +  ROL(a +(H(b,c,d) + Mj + ti), s);
    end;procedure II(var a: Cardinal; b,c,d,Mj,s,ti: Cardinal);
      function I(X, Y, Z: Cardinal): Cardinal;
      begin
        Result := Y xor (X or (not Z));
      end;
    begin
      a := b +  ROL(a +(I(b,c,d) + Mj + ti), s);
    end;procedure MD5Code(var InData; var OutData; cByte: Integer);
    type
      TDWordAry = array [0..3] of Cardinal;
      PDWordAry = ^TDwordAry;
    var
      M: array[0..15] of Cardinal;
      CGroup: Integer;
      i: Integer;
      Va,Vb,Vc,Vd: Cardinal;
      a,b,c,d: Cardinal;
      OldLen: Integer;
    begin
      OldLen := cByte;
      CGroup := (cByte + 1) div 64;
      if (cByte + 1) mod 64 > 56 then
        Inc(CGroup);  Va := Ca;
      Vb := Cb;
      Vc := Cc;
      Vd := Cd;  for i := 0 to CGroup do
      begin
        a := Va;
        b := Vb;
        c := Vc;
        d := Vd;    if i = CGroup then
        begin
          ZeroMemory(@M[0], 64);
          if cByte >= 0 then
          begin
            Move(PByteArray(@InData)^[i * 64], M[0], cByte);
            PByteArray(@M)^[cByte] := $80;
          end;
          M[14] := OldLen shl 3;
          M[15] := OldLen shr 29;
        end
        else
        begin
          if cByte < 64 then
          begin
            Move(PByteArray(@InData)^[i * 64], M[0], cByte);
            PByteArray(@M)^[cByte] := $80;
            ZeroMemory(@PByteArray(@M)^[cByte + 1], 64 - cByte - 1);
          end
          else
            Move(PByteArray(@InData)^[i * 64], M[0], 64);
          Dec(cByte, 64);
        end;
          //第一轮
         FF(a,b,c,d,M[0],7,  T[0,0]);
         FF(d,a,b,c,M[1],12, T[0,1]);
         FF(c,d,a,b,M[2],17, T[0,2]);
         FF(b,c,d,a,M[3],22, T[0,3]);
         FF(a,b,c,d,M[4],7,  T[0,4]);
         FF(d,a,b,c,M[5],12, T[0,5]);
         FF(c,d,a,b,M[6],17, T[0,6]);
         FF(b,c,d,a,M[7],22, T[0,7]);
         FF(a,b,c,d,M[8],7,  T[0,8]);
         FF(d,a,b,c,M[9],12, T[0,9]);
         FF(c,d,a,b,M[10],17,T[0,10]);
         FF(b,c,d,a,M[11],22,T[0,11]);
         FF(a,b,c,d,M[12],7, T[0,12]);
         FF(d,a,b,c,M[13],12,T[0,13]);
         FF(c,d,a,b,M[14],17,T[0,14]);
         FF(b,c,d,a,M[15],22,T[0,15]);
         //第二轮
         GG(a,b,c,d,M[1],5,   T[1,0]);
         GG(d,a,b,c,M[6],9,   T[1,1]);
         GG(c,d,a,b,M[11],14, T[1,2]);
         GG(b,c,d,a,M[0],20,  T[1,3]);
         GG(a,b,c,d,M[5],5,   T[1,4]);
         GG(d,a,b,c,M[10],9,  T[1,5]);
         GG(c,d,a,b,M[15],14, T[1,6]);
         GG(b,c,d,a,M[4],20,  T[1,7]);
         GG(a,b,c,d,M[9],5,   T[1,8]);
         GG(d,a,b,c,M[14],9,  T[1,9]);
         GG(c,d,a,b,M[3],14,  T[1,10]);
         GG(b,c,d,a,M[8],20,  T[1,11]);
         GG(a,b,c,d,M[13],5,  T[1,12]);
         GG(d,a,b,c,M[2],9,   T[1,13]);
         GG(c,d,a,b,M[7],14,  T[1,14]);
         GG(b,c,d,a,M[12],20, T[1,15]);
         //第三轮
         HH(a,b,c,d,M[5],4,   T[2,0]);
         HH(d,a,b,c,M[8],11,  T[2,1]);
         HH(c,d,a,b,M[11],16, T[2,2]);
         HH(b,c,d,a,M[14],23, T[2,3]);
         HH(a,b,c,d,M[1],4,   T[2,4]);
         HH(d,a,b,c,M[4],11,  T[2,5]);
         HH(c,d,a,b,M[7],16,  T[2,6]);
         HH(b,c,d,a,M[10],23, T[2,7]);
         HH(a,b,c,d,M[13],4,  T[2,8]);
         HH(d,a,b,c,M[0],11,  T[2,9]);
         HH(c,d,a,b,M[3],16,  T[2,10]);
         HH(b,c,d,a,M[6],23,  T[2,11]);
         HH(a,b,c,d,M[9],4,   T[2,12]);
         HH(d,a,b,c,M[12],11, T[2,13]);
         HH(c,d,a,b,M[15],16, T[2,14]);
         HH(b,c,d,a,M[2],23,  T[2,15]);
         //第四轮
         II(a,b,c,d,M[0],6,   T[3,0]);
         II(d,a,b,c,M[7],10,  T[3,1]);
         II(c,d,a,b,M[14],15, T[3,2]);
         II(b,c,d,a,M[5],21,  T[3,3]);
         II(a,b,c,d,M[12],6,  T[3,4]);
         II(d,a,b,c,M[3],10,  T[3,5]);
         II(c,d,a,b,M[10],15, T[3,6]);
         II(b,c,d,a,M[1],21,  T[3,7]);
         II(a,b,c,d,M[8],6,   T[3,8]);
         II(d,a,b,c,M[15],10, T[3,9]);
         II(c,d,a,b,M[6],15,  T[3,10]);
         II(b,c,d,a,M[13],21, T[3,11]);
         II(a,b,c,d,M[4],6,   T[3,12]);
         II(d,a,b,c,M[11],10, T[3,13]);
         II(c,d,a,b,M[2],15,  T[3,14]);
         II(b,c,d,a,M[9],21,  T[3,15]);    Va := Va + a;
        Vb := Vb + b;
        Vc := Vc + c;
        Vd := Vd + d;
      end;  PDWordAry(@OutData)^[0] := Va;
      PDWordAry(@OutData)^[1] := Vb;
      PDWordAry(@OutData)^[2] := Vc;
      PDwordAry(@OutData)^[3] := Vd;
    end;
    end.
      

  2.   

    (*********************from:http://www.freesoft.org/CIE/RFC/1321/3.htm***********MD5 Algorithm Description  We begin by supposing that we have a b-bit message as input, and that we wish
    to find its message digest. Here b is an arbitrary nonnegative integer; b may be
    zero, it need not be a multiple of eight, and it may be arbitrarily large. We
    imagine the bits of the message written down as follows:
              m_0 m_1 ... m_{b-1}  The following five steps are performed to compute the message digest of the
    message.Step 1. Append Padding Bits  The message is "padded" (extended) so that its length (in bits) is congruent
    to 448, modulo 512. That is, the message is extended so that it is just 64 bits
    shy of being a multiple of 512 bits long. Padding is always performed, even if
    the length of the message is already congruent to 448, modulo 512.  Padding is performed as follows: a single "1" bit is appended to the message,
    and then "0" bits are appended so that the length in bits of the padded message
    becomes congruent to 448, modulo 512. In all, at least one bit and at most 512
    bits are appended.Step 2. Append Length  A 64-bit representation of b (the length of the message before the padding
    bits were added) is appended to the result of the previous step. In the unlikely
    event that b is greater than 2^64, then only the low-order 64 bits of b are
    used. (These bits are appended as two 32-bit words and appended low-order word
    first in accordance with the previous conventions.)  At this point the resulting message (after padding with bits and with b) has
    a length that is an exact multiple of 512 bits. Equivalently, this message has
    a length that is an exact multiple of 16 (32-bit) words. Let M[0 ... N-1] denote
    the words of the resulting message, where N is a multiple of 16.Step 3. Initialize MD Buffer  A four-word buffer (A,B,C,D) is used to compute the message digest. Here each
    of A, B, C, D is a 32-bit register. These registers are initialized to the
    following values in hexadecimal, low-order bytes first):
              word A: 01 23 45 67
              word B: 89 ab cd ef
              word C: fe dc ba 98
              word D: 76 54 32 10Step 4. Process Message in 16-Word Blocks  We first define four auxiliary functions that each take as input three 32-bit
    words and produce as output one 32-bit word.
       F(X,Y,Z) =(X&Y)|((~X)&Z)
       G(X,Y,Z) =(X&Z)|(Y&(~Z))
       H(X,Y,Z) =X^Y^Z
       I(X,Y,Z)=Y^(X|(~Z))  In each bit position F acts as a conditional: if X then Y else Z. The function
    F could have been defined using + instead of v since XY and not(X)Z will never
    have 1's in the same bit position.) It is interesting to note that if the bits
    of X, Y, and Z are independent and unbiased, the each bit of F(X,Y,Z) will be
    independent and unbiased.  The functions G, H, and I are similar to the function F, in that they act in
    "bitwise parallel" to produce their output from the bits of X, Y, and Z, in such
    a manner that if the corresponding bits of X, Y, and Z are independent and
    unbiased, then each bit of G(X,Y,Z), H(X,Y,Z), and I(X,Y,Z) will be independent
    and unbiased. Note that the function H is the bit-wise "xor" or "parity"
    function of its inputs.  This step uses a 64-element table T[1 ... 64] constructed from the sine
    function. Let T[i] denote the i-th element of the table, which is equal to the
    integer part of 4294967296 times abs(sin(i)), where i is in radians. The
    elements of the table are given in the appendix.Do the following:
       /* Process each 16-word block. */
       For i = 0 to N/16-1 do
         /* Copy block i into X. */
         For j = 0 to 15 do
           Set X[j] to M[i*16+j].
         end /* of loop on j */     /* Save A as AA, B as BB, C as CC, and D as DD. */
         AA = A
         BB = B     CC = C
         DD = D     /* Round 1. */
         /* Let [abcd k s i] denote the operation
              a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
         /* Do the following 16 operations. */
         [ABCD  0  7  1]
         [DABC  1 12  2]
         [CDAB  2 17  3]
         [BCDA  3 22  4]
         [ABCD  4  7  5]
         [DABC  5 12  6]
         [CDAB  6 17  7]
         [BCDA  7 22  8]
         [ABCD  8  7  9]
         [DABC  9 12 10]
         [CDAB 10 17 11]
         [BCDA 11 22 12]
         [ABCD 12  7 13]
         [DABC 13 12 14]
         [CDAB 14 17 15]
         [BCDA 15 22 16]     /* Round 2. */
         /* Let [abcd k s i] denote the operation
              a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
         /* Do the following 16 operations. */
         [ABCD  1  5 17]
         [DABC  6  9 18]
         [CDAB 11 14 19]
         [BCDA  0 20 20]
         [ABCD  5  5 21]
         [DABC 10  9 22]
         [CDAB 15 14 23]
         [BCDA  4 20 24]
         [ABCD  9  5 25]
         [DABC 14  9 26]
         [CDAB  3 14 27]
         [BCDA  8 20 28]
         [ABCD 13  5 29]
         [DABC  2  9 30]
         [CDAB  7 14 31]
         [BCDA 12 20 32]     /* Round 3. */
         /* Let [abcd k s t] denote the operation
              a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
         /* Do the following 16 operations. */
         [ABCD  5  4 33]
         [DABC  8 11 34]
         [CDAB 11 16 35]
         [BCDA 14 23 36]
         [ABCD  1  4 37]
         [DABC  4 11 38]
         [CDAB  7 16 39]
         [BCDA 10 23 40]
         [ABCD 13  4 41]
         [DABC  0 11 42]
         [CDAB  3 16 43]
         [BCDA  6 23 44]
         [ABCD  9  4 45]
         [DABC 12 11 46]
         [CDAB 15 16 47]
         [BCDA  2 23 48]     /* Round 4. */
         /* Let [abcd k s t] denote the operation
              a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
         /* Do the following 16 operations. */
         [ABCD  0  6 49]
         [DABC  7 10 50]
         [CDAB 14 15 51]
         [BCDA  5 21 52]
         [ABCD 12  6 53]
         [DABC  3 10 54]
         [CDAB 10 15 55]
         [BCDA  1 21 56]
         [ABCD  8  6 57]
         [DABC 15 10 58]
         [CDAB  6 15 59]
         [BCDA 13 21 60]
         [ABCD  4  6 61]
         [DABC 11 10 62]
         [CDAB  2 15 63]
         [BCDA  9 21 64]     /* Then perform the following additions. (That is increment each
            of the four registers by the value it had before this block
            was started.) */
         A = A + AA
         B = B + BB
         C = C + CC
         D = D + DD   end /* of loop on i */Step 5. Output  The message digest produced as output is A, B, C, D. That is, we begin with
    the low-order byte of A, and end with the high-order byte of D.  This completes the description of MD5. A reference implementation in C is
    given in the appendix.Summary  The MD5 message-digest algorithm is simple to implement, and provides a
    "fingerprint" or message digest of a message of arbitrary length. It is
    conjectured that the difficulty of coming up with two messages having the same
    message digest is on the order of 2^64 operations, and that the difficulty of
    coming up with any message having a given message digest is on the order of
    2^128 operations. The MD5 algorithm has been carefully scrutinized for
    weaknesses. It is, however, a relatively new algorithm and further security
    analysis is of course justified, as is the case with any new proposal of this
    sort.*******************************************************************************)
      

  3.   

    to  lovefox_zoe(爱情狐狸):
    谢谢你的代码,能否针对你代码的说明一下你的思路,我看起代码来有点费劲。
    或者,给下面几句代码增加注释,谢谢!
    //InData,OutData是什么类型?
    procedure MD5Code(var InData; var OutData; cByte: Integer);//这四个初始值是不是和你下面写的内容不一致,为什么?
    const
       Ca = $67452301;
       Cb = $EFCDAB89;
       Cc = $98BADCFE;
       Cd = $10325476;
      

  4.   

    我就结合一些网络资料翻译一下,MD5加密原理吧。
    一、算法实现
    1、MD5算法是对输入的数据进行补位,使得如果数据位长度LEN对512求余的结果 
    是448。 
    即数据扩展至K*512+448位。即K*64+56个字节,K为整数。 
    具体补位操作:补一个1,然后补0至满足上述要求 
    2、补数据长度: 
    用一个64位的数字表示数据的原始长度B,把B用两个32位数表示。这时,数据 
    就被填 
    补成长度为512位的倍数。 
    3. 初始化MD5参数 
    四个32位整数 (A,B,C,D) 用来计算信息摘要,初始化使用的是十六进制表示 
    的数字 
    A=0X01234567 
    B=0X89abcdef 
    C=0Xfedcba98 
    D=0X76543210 
    4、处理位操作函数 
    X,Y,Z为32位整数。 
    F(X,Y,Z) = X&Y|NOT(X)&Z 
    G(X,Y,Z) = X&Z|Y&not;(Z) 
    H(X,Y,Z) = X xor Y xor Z 
    I(X,Y,Z) = Y xor (X|not(Z)) 
    5、主要变换过程: 
    使用常数组T[1 ... 64], T为32位整数用16进制表示,数据用16个32位的 
    整 
    数数组M[]表示。 
    具体过程如下: 
    /* 处理数据原文 */ 
    For i = 0 to N/16-1 do 
    /*每一次,把数据原文存放在16个元素的数组X中. */ 
    For j = 0 to 15 do 
    Set X[j] to M[i*16+j]. 
    end /结束对J的循环 
    /* Save A as AA, B as BB, C as CC, and D as DD. */ 
    AA = A 
    BB = B 
    CC = C 
    DD = D 
    /* 第1轮*/ 
    /* 以 [abcd k s i]表示如下操作 
    a = b + ((a + F(b,c,d) + X[k] + T) <<< s). */ 
    /* Do the following 16 operations. */ 
    [ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4] 
    [ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8] 
    [ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12] 
    [ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16] 
    /* 第2轮* */ 
    /* 以 [abcd k s i]表示如下操作 
    a = b + ((a + G(b,c,d) + X[k] + T) <<< s). */ 
    /* Do the following 16 operations. */ 
    [ABCD 1 5 17] [DABC 6 9 18] [CDAB 11 14 19] [BCDA 0 20 20] 
    [ABCD 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24] 
    [ABCD 9 5 25] [DABC 14 9 26] [CDAB 3 14 27] [BCDA 8 20 28] 
    [ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32] 
    /* 第3轮*/ 
    /* 以 [abcd k s i]表示如下操作 
    a = b + ((a + H(b,c,d) + X[k] + T) <<< s). */ 
    /* Do the following 16 operations. */ 
    [ABCD 5 4 33] [DABC 8 11 34] [CDAB 11 16 35] [BCDA 14 23 36] 
    [ABCD 1 4 37] [DABC 4 11 38] [CDAB 7 16 39] [BCDA 10 23 40] 
    [ABCD 13 4 41] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44] 
    [ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48] 
    /* 第4轮*/ 
    /* 以 [abcd k s i]表示如下操作 
    a = b + ((a + I(b,c,d) + X[k] + T) <<< s). */ 
    /* Do the following 16 operations. */ 
    [ABCD 0 6 49] [DABC 7 10 50] [CDAB 14 15 51] [BCDA 5 21 52] 
    [ABCD 12 6 53] [DABC 3 10 54] [CDAB 10 15 55] [BCDA 1 21 56] 
    [ABCD 8 6 57] [DABC 15 10 58] [CDAB 6 15 59] [BCDA 13 21 60] 
    [ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] [BCDA 9 21 64] 
    /* 然后进行如下操作 */ 
    A = A + AA 
    B = B + BB 
    C = C + CC 
    D = D + DD 
    end /* 结束对I的循环*/ 
    6、输出结果。
      

  5.   

    to lovefox_zoe(爱情狐狸):非常感谢你的回答,我麻烦你一下,给我一个通过使用MD5Code过程加密的例子,包括对字符串和文件加密,因为我对下面的过程中的InData和OutData的用法不了解。procedure MD5Code(var InData; var OutData; cByte: Integer);
      

  6.   

    to lovefox_zoe:你给我的代码由于没有注释,我看起来很不容易,能不能给下面的代码加上注释,否则我真是一头雾水。
    OldLen := cByte;
      CGroup := (cByte + 1) div 64;     //为什么cByte加1
      if (cByte + 1) mod 64 > 56 then   //这是什么意思
        Inc(CGroup);
    if i = CGroup then    
        begin
          ZeroMemory(@M[0], 64);
          if cByte >= 0 then
          begin
            Move(PByteArray(@InData)^[i * 64], M[0], cByte);
            PByteArray(@M)^[cByte] := $80;
          end;
          M[14] := OldLen shl 3;      //这两句的目的是什么?
          M[15] := OldLen shr 29;
        end
        else
        begin
          if cByte < 64 then
          begin
            Move(PByteArray(@InData)^[i * 64], M[0], cByte);
            PByteArray(@M)^[cByte] := $80;
            ZeroMemory(@PByteArray(@M)^[cByte + 1], 64 - cByte - 1);
          end
          else
            Move(PByteArray(@InData)^[i * 64], M[0], 64);
          Dec(cByte, 64);
        end;谢谢!