MD5算法详细介绍

MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest开发出来,经MD2、MD3和MD4发展而来。它的作用是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的大整数)。不管是MD2、MD4还是MD5,它们都需要获得一个随机长度的信息并产生一个128位的信息摘要。虽然这些算法的结构或多或少有些相似,但MD2的设计与MD4和MD5完全不同,那是因为MD2是为8位机器做过设计优化的,而MD4和MD5却是面向32位的电脑。这三个算法的描述和C语言源代码在Internet RFCs 1321中有详细的描述http://www.ietf.org/rfc/rfc1321.txt),这是一份最权威的文档,由Ronald L. Rivest在1992年8月向IEFT提交。

  Rivest在1989年开发出MD2算法。在这个算法中,首先对信息进行数据补位,使信息的字节长度是16的倍数。然后,以一个16位的检验和追加到信息末尾。并且根据这个新产生的信息计算出散列值。后来,Rogier和Chauvaud发现如果忽略了检验和将产生MD2冲突。MD2算法的加密后结果是唯一的--既没有重复。

  为了加强算法的安全性,Rivest在1990年又开发出MD4算法。MD4算法同样需要填补信息以确保信息的字节长度加上448后能被512整除(信息字节长度mod 512 = 448)。然后,一个以64位二进制表示的信息的最初长度被添加进来。信息被处理成512位Damg?rd/Merkle迭代结构的区块,而且每个区块要通过三个不同步骤的处理。Den Boer和Bosselaers以及其他人很快的发现了攻击MD4版本中第一步和第三步的漏洞。Dobbertin向大家演示了如何利用一部普通的个人电脑在几分钟内找到MD4完整版本中的冲突(这个冲突实际上是一种漏洞,它将导致对不同的内容进行加密却可能得到相同的加密后结果)。毫无疑问,MD4就此被淘汰掉了。

  尽管MD4算法在安全上有个这么大的漏洞,但它对在其后才被开发出来的好几种信息安全加密算法的出现却有着不可忽视的引导作用。除了MD5以外,其中比较有名的还有SHA-1、RIPE-MD以及HAVAL等。

  一年以后,即1991年,Rivest开发出技术上更为趋近成熟的MD5算法。它在MD4的基础上增加了"安全-带子"(Safety-Belts)的概念。虽然MD5比MD4稍微慢一些,但却更为安全。这个算法很明显的由四个和MD4设计有少许不同的步骤组成。在MD5算法中,信息-摘要的大小和填充的必要条件与MD4完全相同。Den Boer和Bosselaers曾发现MD5算法中的假冲突(Pseudo-Collisions),但除此之外就没有其他被发现的加密后结果了。

  Van Oorschot和Wiener曾经考虑过一个在散列中暴力搜寻冲突的函数(Brute-Force Hash Function),而且他们猜测一个被设计专门用来搜索MD5冲突的机器(这台机器在1994年的制造成本大约是一百万美元)可以平均每24天就找到一个冲突。但单从1991年到2001年这10年间,竟没有出现替代MD5算法的MD6或被叫做其他什么名字的新算法这一点,我们就可以看出这个瑕疵并没有太多的影响MD5的安全性。上面所有这些都不足以成为MD5的在实际应用中的问题。并且,由于MD5算法的使用不需要支付任何版权费用的,所以在一般的情况下(非绝密应用领域。但即便是应用在绝密领域内,MD5也不失为一种非常优秀的中间技术),MD5怎么都应该算得上是非常安全的了。

  在一些初始化处理后,MD5以512位分组来处理输入文本,每一分组又划分为16个32位子分组。算法的输出由四个32位分组组成,将它们级联形成一个128位散列值。

  首先填充消息使其长度恰好为一个比512位的倍数仅小64位的数。填充方法是附一个1在消息后面,后接所要求的多个0,然后在其后附上64位的消息长度(填充前)。这两步的作用是使消息长度恰好是512位的整数倍(算法的其余部分要求如此),同时确保不同的消息在填充后不相同。

  四个32位变量初始化为:

  A=0x01234567

  B=0x89abcdef

  C=0xfedcba98

  D=0x76543210

  它们称为链接变量(chaining variable)

  接着进行算法的主循环,循环的次数是消息中512位消息分组的数目。

  将上面四个变量复制到别外的变量中:A到a,B到b,C到c,D到d。

  主循环有四轮(MD4只有三轮),每轮很相拟。第一轮进行16次操作。每次操作对a,b,c和d中的其中三个作一次非线性函数运算,然后将所得结果加上第四个变量,文本的一个子分组和一个常数。再将所得结果向右环移一个不定的数,并加上a,b,c或d中之一。最后用该结果取代a,b,c或d中之一。

  以一下是每次操作中用到的四个非线性函数(每轮一个)。

  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))

  (&是与,|是或,~是非,^是异或)

  这些函数是这样设计的:如果X、Y和Z的对应位是独立和均匀的,那么结果的每一位也应是独立和均匀的。

  函数F是按逐位方式操作:如果X,那么Y,否则Z。函数H是逐位奇偶操作符。

  设Mj表示消息的第j个子分组(从0到15),<<<s表示循环左移s位,则四种操作为:

  FF(a,b,c,d,Mj,s,ti)表示a=b+((a+(F(b,c,d)+Mj+ti)<<<s)

  GG(a,b,c,d,Mj,s,ti)表示a=b+((a+(G(b,c,d)+Mj+ti)<<<s)

  HH(a,b,c,d,Mj,s,ti)表示a=b+((a+(H(b,c,d)+Mj+ti)<<<s)

  II(a,b,c,d,Mj,s,ti)表示a=b+((a+(I(b,c,d)+Mj+ti)<<<s)

  这四轮(64步)是:

  第一轮

  FF(a,b,c,d,M0,7,0xd76aa478)

  FF(d,a,b,c,M1,12,0xe8c7b756)

  FF(c,d,a,b,M2,17,0x242070db)

  FF(b,c,d,a,M3,22,0xc1bdceee)

  FF(a,b,c,d,M4,7,0xf57c0faf)

  FF(d,a,b,c,M5,12,0x4787c62a)

  FF(c,d,a,b,M6,17,0xa8304613)

  FF(b,c,d,a,M7,22,0xfd469501)

  FF(a,b,c,d,M8,7,0x698098d8)

  FF(d,a,b,c,M9,12,0x8b44f7af)

  FF(c,d,a,b,M10,17,0xffff5bb1)

  FF(b,c,d,a,M11,22,0x895cd7be)

  FF(a,b,c,d,M12,7,0x6b901122)

  FF(d,a,b,c,M13,12,0xfd987193)

  FF(c,d,a,b,M14,17,0xa679438e)

  FF(b,c,d,a,M15,22,0x49b40821)

  第二轮

  GG(a,b,c,d,M1,5,0xf61e2562)

  GG(d,a,b,c,M6,9,0xc040b340)

  GG(c,d,a,b,M11,14,0x265e5a51)

  GG(b,c,d,a,M0,20,0xe9b6c7aa)

  GG(a,b,c,d,M5,5,0xd62f105d)

  GG(d,a,b,c,M10,9,0x02441453)

  GG(c,d,a,b,M15,14,0xd8a1e681)

  GG(b,c,d,a,M4,20,0xe7d3fbc8)

  GG(a,b,c,d,M9,5,0x21e1cde6)

  GG(d,a,b,c,M14,9,0xc33707d6)

  GG(c,d,a,b,M3,14,0xf4d50d87)

  GG(b,c,d,a,M8,20,0x455a14ed)

  GG(a,b,c,d,M13,5,0xa9e3e905)

  GG(d,a,b,c,M2,9,0xfcefa3f8)

  GG(c,d,a,b,M7,14,0x676f02d9)

  GG(b,c,d,a,M12,20,0x8d2a4c8a)

  第三轮

  HH(a,b,c,d,M5,4,0xfffa3942)

  HH(d,a,b,c,M8,11,0x8771f681)

  HH(c,d,a,b,M11,16,0x6d9d6122)

  HH(b,c,d,a,M14,23,0xfde5380c)

  HH(a,b,c,d,M1,4,0xa4beea44)

  HH(d,a,b,c,M4,11,0x4bdecfa9)

  HH(c,d,a,b,M7,16,0xf6bb4b60)

  HH(b,c,d,a,M10,23,0xbebfbc70)

  HH(a,b,c,d,M13,4,0x289b7ec6)

  HH(d,a,b,c,M0,11,0xeaa127fa)

  HH(c,d,a,b,M3,16,0xd4ef3085)

  HH(b,c,d,a,M6,23,0x04881d05)

  HH(a,b,c,d,M9,4,0xd9d4d039)

  HH(d,a,b,c,M12,11,0xe6db99e5)

  HH(c,d,a,b,M15,16,0x1fa27cf8)

  HH(b,c,d,a,M2,23,0xc4ac5665)

  第四轮

  II(a,b,c,d,M0,6,0xf4292244)

  II(d,a,b,c,M7,10,0x432aff97)

  II(c,d,a,b,M14,15,0xab9423a7)

  II(b,c,d,a,M5,21,0xfc93a039)

  II(a,b,c,d,M12,6,0x655b59c3)

  II(d,a,b,c,M3,10,0x8f0ccc92)

  II(c,d,a,b,M10,15,0xffeff47d)

  II(b,c,d,a,M1,21,0x85845dd1)

  II(a,b,c,d,M8,6,0x6fa87e4f)

  II(d,a,b,c,M15,10,0xfe2ce6e0)

  II(c,d,a,b,M6,15,0xa3014314)

  II(b,c,d,a,M13,21,0x4e0811a1)

  II(a,b,c,d,M4,6,0xf7537e82)

  II(d,a,b,c,M11,10,0xbd3af235)

  II(c,d,a,b,M2,15,0x2ad7d2bb)

  II(b,c,d,a,M9,21,0xeb86d391)

  常数ti可以如下选择:

  在第i步中,ti是4294967296*abs(sin(i))的整数部分,i的单位是弧度。

  (2的32次方)

  所有这些完成之后,将A,B,C,D分别加上a,b,c,d。然后用下一分组数据继续运行算法,最后的输出是A,B,C和D的级联。

//--------------------------以下是adobe 的 AS3的MD5加密类的例子---------------------------------

 

 
 
package com.adobe.crypto {
 
 import com.adobe.utils.IntUtil;
 import flash.utils.ByteArray; 
 /**
  * The MD5 Message-Digest Algorithm
  *
  * Implementation based on algorithm description at 
  * http://www.faqs.org/rfcs/rfc1321.html
  */
 public class MD5 {
  
  public static var digest:ByteArray;
  /**
   * Performs the MD5 hash algorithm on a string.
   *
   * @param s The string to hash
   * @return A string containing the hash value of s
   * @langversion ActionScript 3.0
   * @playerversion Flash 8.5
   * @tiptext
   */
   
  public static function hash(s:String) :String{
   //Convert to byteArray and send through hashBinary function
   // so as to only have complex code in one location
   var ba:ByteArray = new ByteArray();
   ba.writeUTFBytes(s); 
   return hashBinary(ba);
  }
  
  public static function hashBytes(s:ByteArray) :String{ 
   return hashBinary(s);
  }
  
  /**
   * Performs the MD5 hash algorithm on a ByteArray.
   *
   * @param s The string to hash
   * @return A string containing the hash value of s
   * @langversion ActionScript 3.0
   * @playerversion Flash 8.5
   * @tiptext
   */  
  public static function hashBinary( s:ByteArray ):String {
   // initialize the md buffers
   var a:int = 1732584193;
   var b:int = -271733879;
   var c:int = -1732584194;
   var d:int = 271733878;
   
   // variables to store previous values
   var aa:int;
   var bb:int;
   var cc:int;
   var dd:int;
   
   // create the blocks from the string and
   // save the length as a local var to reduce
   // lookup in the loop below
   var x:Array = createBlocks( s );
   var len:int = x.length;
   
   // loop over all of the blocks
   for ( var i:int = 0; i < len; i += 16) {
    // save previous values
    aa = a;
    bb = b;
    cc = c;
    dd = d;    
    
    // Round 1
    a = ff( a, b, c, d, x[int(i+ 0)],  7, -680876936 );  // 1
    d = ff( d, a, b, c, x[int(i+ 1)], 12, -389564586 ); // 2
    c = ff( c, d, a, b, x[int(i+ 2)], 17, 606105819 );  // 3
    b = ff( b, c, d, a, x[int(i+ 3)], 22, -1044525330 ); // 4
    a = ff( a, b, c, d, x[int(i+ 4)],  7, -176418897 );  // 5
    d = ff( d, a, b, c, x[int(i+ 5)], 12, 1200080426 );  // 6
    c = ff( c, d, a, b, x[int(i+ 6)], 17, -1473231341 ); // 7
    b = ff( b, c, d, a, x[int(i+ 7)], 22, -45705983 );  // 8
    a = ff( a, b, c, d, x[int(i+ 8)],  7, 1770035416 );  // 9
    d = ff( d, a, b, c, x[int(i+ 9)], 12, -1958414417 ); // 10
    c = ff( c, d, a, b, x[int(i+10)], 17, -42063 );   // 11
    b = ff( b, c, d, a, x[int(i+11)], 22, -1990404162 ); // 12
    a = ff( a, b, c, d, x[int(i+12)],  7, 1804603682 );  // 13
    d = ff( d, a, b, c, x[int(i+13)], 12, -40341101 );  // 14
    c = ff( c, d, a, b, x[int(i+14)], 17, -1502002290 ); // 15
    b = ff( b, c, d, a, x[int(i+15)], 22, 1236535329 );  // 16
    
    // Round 2
    a = gg( a, b, c, d, x[int(i+ 1)],  5, -165796510 );  // 17
    d = gg( d, a, b, c, x[int(i+ 6)],  9, -1069501632 ); // 18
    c = gg( c, d, a, b, x[int(i+11)], 14, 643717713 );  // 19
    b = gg( b, c, d, a, x[int(i+ 0)], 20, -373897302 );  // 20
    a = gg( a, b, c, d, x[int(i+ 5)],  5, -701558691 );  // 21
    d = gg( d, a, b, c, x[int(i+10)],  9, 38016083 );  // 22
    c = gg( c, d, a, b, x[int(i+15)], 14, -660478335 );  // 23
    b = gg( b, c, d, a, x[int(i+ 4)], 20, -405537848 );  // 24
    a = gg( a, b, c, d, x[int(i+ 9)],  5, 568446438 );  // 25
    d = gg( d, a, b, c, x[int(i+14)],  9, -1019803690 ); // 26
    c = gg( c, d, a, b, x[int(i+ 3)], 14, -187363961 );  // 27
    b = gg( b, c, d, a, x[int(i+ 8)], 20, 1163531501 );  // 28
    a = gg( a, b, c, d, x[int(i+13)],  5, -1444681467 ); // 29
    d = gg( d, a, b, c, x[int(i+ 2)],  9, -51403784 );  // 30
    c = gg( c, d, a, b, x[int(i+ 7)], 14, 1735328473 );  // 31
    b = gg( b, c, d, a, x[int(i+12)], 20, -1926607734 ); // 32
    
    // Round 3
    a = hh( a, b, c, d, x[int(i+ 5)],  4, -378558 );  // 33
    d = hh( d, a, b, c, x[int(i+ 8)], 11, -2022574463 ); // 34
    c = hh( c, d, a, b, x[int(i+11)], 16, 1839030562 );  // 35
    b = hh( b, c, d, a, x[int(i+14)], 23, -35309556 );  // 36
    a = hh( a, b, c, d, x[int(i+ 1)],  4, -1530992060 ); // 37
    d = hh( d, a, b, c, x[int(i+ 4)], 11, 1272893353 );  // 38
    c = hh( c, d, a, b, x[int(i+ 7)], 16, -155497632 );  // 39
    b = hh( b, c, d, a, x[int(i+10)], 23, -1094730640 ); // 40
    a = hh( a, b, c, d, x[int(i+13)],  4, 681279174 );  // 41
    d = hh( d, a, b, c, x[int(i+ 0)], 11, -358537222 );  // 42
    c = hh( c, d, a, b, x[int(i+ 3)], 16, -722521979 );  // 43
    b = hh( b, c, d, a, x[int(i+ 6)], 23, 76029189 );  // 44
    a = hh( a, b, c, d, x[int(i+ 9)],  4, -640364487 );  // 45
    d = hh( d, a, b, c, x[int(i+12)], 11, -421815835 );  // 46
    c = hh( c, d, a, b, x[int(i+15)], 16, 530742520 );  // 47
    b = hh( b, c, d, a, x[int(i+ 2)], 23, -995338651 );  // 48
    
    // Round 4
    a = ii( a, b, c, d, x[int(i+ 0)],  6, -198630844 );  // 49
    d = ii( d, a, b, c, x[int(i+ 7)], 10, 1126891415 );  // 50
    c = ii( c, d, a, b, x[int(i+14)], 15, -1416354905 ); // 51
    b = ii( b, c, d, a, x[int(i+ 5)], 21, -57434055 );  // 52
    a = ii( a, b, c, d, x[int(i+12)],  6, 1700485571 );  // 53
    d = ii( d, a, b, c, x[int(i+ 3)], 10, -1894986606 ); // 54
    c = ii( c, d, a, b, x[int(i+10)], 15, -1051523 );  // 55
    b = ii( b, c, d, a, x[int(i+ 1)], 21, -2054922799 ); // 56
    a = ii( a, b, c, d, x[int(i+ 8)],  6, 1873313359 );  // 57
    d = ii( d, a, b, c, x[int(i+15)], 10, -30611744 );  // 58
    c = ii( c, d, a, b, x[int(i+ 6)], 15, -1560198380 ); // 59
    b = ii( b, c, d, a, x[int(i+13)], 21, 1309151649 );  // 60
    a = ii( a, b, c, d, x[int(i+ 4)],  6, -145523070 );  // 61
    d = ii( d, a, b, c, x[int(i+11)], 10, -1120210379 ); // 62
    c = ii( c, d, a, b, x[int(i+ 2)], 15, 718787259 );  // 63
    b = ii( b, c, d, a, x[int(i+ 9)], 21, -343485551 );  // 64

    a += aa;
    b += bb;
    c += cc;
    d += dd;
   }
   digest = new ByteArray()
   digest.writeInt(a);
   digest.writeInt(b);
   digest.writeInt(c);
   digest.writeInt(d);
   digest.position = 0;
   // Finish up by concatening the buffers with their hex output
   return IntUtil.toHex( a ) + IntUtil.toHex( b ) + IntUtil.toHex( c ) + IntUtil.toHex( d );
  }
  
  /**
   * Auxiliary function f as defined in RFC
   */
  private static function f( x:int, y:int, z:int ):int {
   return ( x & y ) | ( (~x) & z );
  }
  
  /**
   * Auxiliary function g as defined in RFC
   */
  private static function g( x:int, y:int, z:int ):int {
   return ( x & z ) | ( y & (~z) );
  }
  
  /**
   * Auxiliary function h as defined in RFC
   */
  private static function h( x:int, y:int, z:int ):int {
   return x ^ y ^ z;
  }
  
  /**
   * Auxiliary function i as defined in RFC
   */
  private static function i( x:int, y:int, z:int ):int {
   return y ^ ( x | (~z) );
  }
  
  /**
   * A generic transformation function.  The logic of ff, gg, hh, and
   * ii are all the same, minus the function used, so pull that logic
   * out and simplify the method bodies for the transoformation functions.
   */
  private static function transform( func:Function, a:int, b:int, c:int, d:int, x:int, s:int, t:int):int {
   var tmp:int = a + int( func( b, c, d ) ) + x + t;
   return IntUtil.rol( tmp, s ) +  b;
  }
  
  /**
   * ff transformation function
   */
  private static function ff ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
   return transform( f, a, b, c, d, x, s, t );
  }
  
  /**
   * gg transformation function
   */
  private static function gg ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
   return transform( g, a, b, c, d, x, s, t );
  }
  
  /**
   * hh transformation function
   */
  private static function hh ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
   return transform( h, a, b, c, d, x, s, t );
  }
  
  /**
   * ii transformation function
   */
  private static function ii ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
   return transform( i, a, b, c, d, x, s, t );
  }
  
  /**
   * Converts a string to a sequence of 16-word blocks
   * that we'll do the processing on.  Appends padding
   * and length in the process.
   *
   * @param s The string to split into blocks
   * @return An array containing the blocks that s was
   *   split into.
   */
  private static function createBlocks( s:ByteArray ):Array {
   var blocks:Array = new Array();
   var len:int = s.length * 8;
   var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
   for( var i:int = 0; i < len; i += 8 ) {
    blocks[ int(i >> 5) ] |= ( s[ i / 8 ] & mask ) << ( i % 32 );
   }
   
   // append padding and length
   blocks[ int(len >> 5) ] |= 0x80 << ( len % 32 );
   blocks[ int(( ( ( len + 64 ) >>> 9 ) << 4 ) + 14) ] = len;
   return blocks;
  }
  
 }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值