1. /************************************************ 
  2. MD5 算法的Java Bean 
  3. @author:killsun:170781 
  4. Last Modified:10,Mar,2002 
  5. *************************************************/
  6. package
  7. import
  8. /************************************************* 
  9. md5 类实现了RSA Data Security, Inc.在提交给IETF 
  10. 的RFC1321中的MD5 message-digest 算法。 
  11. *************************************************/
  12.    
  13. /************************************************ 
  14. 引用方法: 
  15. 要import com.XXX.util.MD5; 
  16. 然后 
  17. String loginPassword;//存倒数据库中的密码 
  18. String s;//用户输入的密码 
  19. MD5 oMD5= new MD5(); 
  20. String pwdmd5 = oMD5.getMD5ofStr(s);   
  21. loginPassword=pwdmd5; 
  22.  
  23. *************************************************/
  24. public class
  25. /* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 
  26.  这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个 
  27.  Instance间共享*/
  28. static final int S11 = 7;   
  29. static final int S12 = 12;   
  30. static final int S13 = 17;   
  31. static final int S14 = 22;   
  32.    
  33. static final int S21 = 5;   
  34. static final int S22 = 9;   
  35. static final int S23 = 14;   
  36. static final int S24 = 20;   
  37.    
  38. static final int S31 = 4;   
  39. static final int S32 = 11;   
  40. static final int S33 = 16;   
  41. static final int S34 = 23;   
  42.    
  43. static final int S41 = 6;   
  44. static final int S42 = 10;   
  45. static final int S43 = 15;   
  46. static final int S44 = 21;   
  47.    
  48. static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0,   
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  52. /* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中 
  53.            被定义到MD5_CTX结构中 
  54.          
  55.          */
  56. private long[] state = new long[4];  // state (ABCD) 
  57. private long[] count = new long[2];  // number of bits, modulo 2^64 (lsb first) 
  58. private byte[] buffer = new byte[64]; // input buffer 
  59.            
  60. /* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的 
  61.    16进制ASCII表示. 
  62.  */
  63. public
  64.            
  65. /* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值. 
  66.  */
  67. private byte[] digest = new byte[16];   
  68.            
  69. /* 
  70.    getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串 
  71.    返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的. 
  72.  */
  73. public
  74.                 md5Init();   
  75.                 md5Update(inbuf.getBytes(), inbuf.length());   
  76.                 md5Final();   
  77. "";   
  78. for (int i = 0; i < 16; i++) {   
  79.                         digestHexStr += byteHEX(digest[i]);   
  80.                 }   
  81. return
  82.    
  83.         }   
  84. // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数 
  85. public
  86.                 md5Init();   
  87.    
  88. return;   
  89.         }   
  90.            
  91.    
  92.    
  93. /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */
  94. private void
  95. 0] = 0L;   
  96. 1] = 0L;   
  97. ///* Load magic initialization constants. 
  98.    
  99. 0] = 0x67452301L;   
  100. 1] = 0xefcdab89L;   
  101. 2] = 0x98badcfeL;   
  102. 3] = 0x10325476L;   
  103.    
  104. return;   
  105.         }   
  106. 4个基本的MD5函数,在原始的MD5的C实现中,由于它们是   
  107.         简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们   
  108. private方法,名字保持了原来C中的。 */   
  109.    
  110. private long F(long x, long y, long
  111. return
  112.    
  113.         }   
  114. private long G(long x, long y, long
  115. return
  116.    
  117.         }   
  118. private long H(long x, long y, long
  119. return
  120.         }   
  121.    
  122. private long I(long x, long y, long
  123. return
  124.         }   
  125.            
  126. /*  
  127.           FF,GG,HH和II将调用F,G,H,I进行近一步变换 
  128.           FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 
  129.           Rotation is separate from addition to prevent recomputation. 
  130.        */
  131.    
  132. private long FF(long a, long b, long c, long d, long x, long
  133. long
  134.                 a += F (b, c, d) + x + ac;   
  135. int) a << s) | ((int) a >>> (32
  136.                 a += b;   
  137. return
  138.         }   
  139.    
  140. private long GG(long a, long b, long c, long d, long x, long
  141. long
  142.                 a += G (b, c, d) + x + ac;   
  143. int) a << s) | ((int) a >>> (32
  144.                 a += b;   
  145. return
  146.         }   
  147. private long HH(long a, long b, long c, long d, long x, long
  148. long
  149.                 a += H (b, c, d) + x + ac;   
  150. int) a << s) | ((int) a >>> (32
  151.                 a += b;   
  152. return
  153.         }   
  154. private long II(long a, long b, long c, long d, long x, long
  155. long
  156.                 a += I (b, c, d) + x + ac;   
  157. int) a << s) | ((int) a >>> (32
  158.                 a += b;   
  159. return
  160.         }   
  161. /* 
  162.          md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个 
  163.          函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的 
  164.         */
  165. private void md5Update(byte[] inbuf, int
  166.    
  167. int
  168. byte[] block = new byte[64];   
  169. int)(count[0] >>> 3) & 0x3F;   
  170. // /* Update number of bits */ 
  171.    
  172. if ((count[0] += (inputLen << 3)) < (inputLen << 3))   
  173. 1]++;   
  174. 1] += (inputLen >>> 29);   
  175.    
  176. 64
  177.    
  178. // Transform as many times as possible. 
  179. if
  180. 0, partLen);   
  181.                         md5Transform(buffer);   
  182.    
  183. for (i = partLen; i + 63 < inputLen; i += 64) {   
  184.    
  185. 0, i, 64);   
  186.                                 md5Transform (block);   
  187.                         }   
  188. 0;   
  189.    
  190. else
  191.    
  192. 0;   
  193.    
  194. ///* Buffer remaining input */ 
  195.                 md5Memcpy(buffer, inbuf, index, i, inputLen - i);   
  196.    
  197.         }   
  198.            
  199. /* 
  200.           md5Final整理和填写输出结果 
  201.         */
  202. private void
  203. byte[] bits = new byte[8];   
  204. int
  205.    
  206. ///* Save number of bits */ 
  207. 8);   
  208.    
  209. ///* Pad out to 56 mod 64. 
  210. int)(count[0] >>> 3) & 0x3f;   
  211. 56) ? (56 - index) : (120
  212.                 md5Update (PADDING, padLen);   
  213.    
  214. ///* Append length (before padding) */ 
  215. 8);   
  216.    
  217. ///* Store state in digest */ 
  218. 16);   
  219.    
  220.         }   
  221.             
  222. /* md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的 
  223.       字节拷贝到output的outpos位置开始  
  224.         */
  225.    
  226. private void md5Memcpy (byte[] output, byte[] input,   
  227. int outpos, int inpos, int
  228.         {   
  229. int
  230.    
  231. for (i = 0; i < len; i++)   
  232.                         output[outpos + i] = input[inpos + i];   
  233.         }   
  234.            
  235. /* 
  236.            md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节 
  237.         */
  238. private void md5Transform (byte
  239. long a = state[0], b = state[1], c = state[2], d = state[3];   
  240. long[] x = new long[16];   
  241.    
  242. 64);   
  243.    
  244. /* Round 1 */
  245. 0], S11, 0xd76aa478L); /* 1 */
  246. 1], S12, 0xe8c7b756L); /* 2 */
  247. 2], S13, 0x242070dbL); /* 3 */
  248. 3], S14, 0xc1bdceeeL); /* 4 */
  249. 4], S11, 0xf57c0fafL); /* 5 */
  250. 5], S12, 0x4787c62aL); /* 6 */
  251. 6], S13, 0xa8304613L); /* 7 */
  252. 7], S14, 0xfd469501L); /* 8 */
  253. 8], S11, 0x698098d8L); /* 9 */
  254. 9], S12, 0x8b44f7afL); /* 10 */
  255. 10], S13, 0xffff5bb1L); /* 11 */
  256. 11], S14, 0x895cd7beL); /* 12 */
  257. 12], S11, 0x6b901122L); /* 13 */
  258. 13], S12, 0xfd987193L); /* 14 */
  259. 14], S13, 0xa679438eL); /* 15 */
  260. 15], S14, 0x49b40821L); /* 16 */
  261.    
  262. /* Round 2 */
  263. 1], S21, 0xf61e2562L); /* 17 */
  264. 6], S22, 0xc040b340L); /* 18 */
  265. 11], S23, 0x265e5a51L); /* 19 */
  266. 0], S24, 0xe9b6c7aaL); /* 20 */
  267. 5], S21, 0xd62f105dL); /* 21 */
  268. 10], S22, 0x2441453L); /* 22 */
  269. 15], S23, 0xd8a1e681L); /* 23 */
  270. 4], S24, 0xe7d3fbc8L); /* 24 */
  271. 9], S21, 0x21e1cde6L); /* 25 */
  272. 14], S22, 0xc33707d6L); /* 26 */
  273. 3], S23, 0xf4d50d87L); /* 27 */
  274. 8], S24, 0x455a14edL); /* 28 */
  275. 13], S21, 0xa9e3e905L); /* 29 */
  276. 2], S22, 0xfcefa3f8L); /* 30 */
  277. 7], S23, 0x676f02d9L); /* 31 */
  278. 12], S24, 0x8d2a4c8aL); /* 32 */
  279.    
  280. /* Round 3 */
  281. 5], S31, 0xfffa3942L); /* 33 */
  282. 8], S32, 0x8771f681L); /* 34 */
  283. 11], S33, 0x6d9d6122L); /* 35 */
  284. 14], S34, 0xfde5380cL); /* 36 */
  285. 1], S31, 0xa4beea44L); /* 37 */
  286. 4], S32, 0x4bdecfa9L); /* 38 */
  287. 7], S33, 0xf6bb4b60L); /* 39 */
  288. 10], S34, 0xbebfbc70L); /* 40 */
  289. 13], S31, 0x289b7ec6L); /* 41 */
  290. 0], S32, 0xeaa127faL); /* 42 */
  291. 3], S33, 0xd4ef3085L); /* 43 */
  292. 6], S34, 0x4881d05L); /* 44 */
  293. 9], S31, 0xd9d4d039L); /* 45 */
  294. 12], S32, 0xe6db99e5L); /* 46 */
  295. 15], S33, 0x1fa27cf8L); /* 47 */
  296. 2], S34, 0xc4ac5665L); /* 48 */
  297.    
  298. /* Round 4 */
  299. 0], S41, 0xf4292244L); /* 49 */
  300. 7], S42, 0x432aff97L); /* 50 */
  301. 14], S43, 0xab9423a7L); /* 51 */
  302. 5], S44, 0xfc93a039L); /* 52 */
  303. 12], S41, 0x655b59c3L); /* 53 */
  304. 3], S42, 0x8f0ccc92L); /* 54 */
  305. 10], S43, 0xffeff47dL); /* 55 */
  306. 1], S44, 0x85845dd1L); /* 56 */
  307. 8], S41, 0x6fa87e4fL); /* 57 */
  308. 15], S42, 0xfe2ce6e0L); /* 58 */
  309. 6], S43, 0xa3014314L); /* 59 */
  310. 13], S44, 0x4e0811a1L); /* 60 */
  311. 4], S41, 0xf7537e82L); /* 61 */
  312. 11], S42, 0xbd3af235L); /* 62 */
  313. 2], S43, 0x2ad7d2bbL); /* 63 */
  314. 9], S44, 0xeb86d391L); /* 64 */
  315.    
  316. 0] += a;   
  317. 1] += b;   
  318. 2] += c;   
  319. 3] += d;   
  320.    
  321.         }   
  322.            
  323. /*Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 
  324.           只拆低32bit,以适应原始C实现的用途 
  325.         */
  326. private void Encode (byte[] output, long[] input, int
  327. int
  328.    
  329. for (i = 0, j = 0; j < len; i++, j += 4) {   
  330. byte)(input[i] & 0xffL);   
  331. 1] = (byte)((input[i] >>> 8) & 0xffL);   
  332. 2] = (byte)((input[i] >>> 16) & 0xffL);   
  333. 3] = (byte)((input[i] >>> 24) & 0xffL);   
  334.                 }   
  335.         }   
  336.    
  337. /*Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的, 
  338.           只合成低32bit,高32bit清零,以适应原始C实现的用途 
  339.         */
  340. private void Decode (long[] output, byte[] input, int
  341. int
  342.    
  343.    
  344. for (i = 0, j = 0; j < len; i++, j += 4)   
  345.                         output[i] = b2iu(input[j]) |   
  346. 1]) << 8) |   
  347. 2]) << 16) |   
  348. 3]) << 24);   
  349.    
  350. return;   
  351.         }   
  352.           
  353. /* 
  354.           b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算 
  355.         */
  356. public static long b2iu(byte
  357. return b < 0 ? b & 0x7F + 128
  358.         }   
  359.            
  360. /*byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示, 
  361.   因为java中的byte的toString无法实现这一点,我们又没有C语言中的 
  362.    sprintf(outbuf,"%02X",ib) 
  363.  */
  364. public static String byteHEX(byte
  365. char[] Digit = { '0','1','2','3','4','5','6','7','8','9',   
  366. 'a','b','c','d','e','f'
  367. char [] ob = new char[2];   
  368. 0] = Digit[(ib >>> 4) & 0X0F];   
  369. 1] = Digit[ib & 0X0F];   
  370. new
  371. return
  372.         }   
  373.    
  374. public static void
  375.    
  376.    
  377. new
  378. if (Array.getLength(args) == 0) {   //如果没有参数,执行标准的Test Suite 
  379.                    
  380. "MD5 Test suite:");   
  381. "MD5(/"10/"):"+m.getMD5ofStr("355694005457784"));   
  382. "MD5(/"11/"):"+m.getMD5ofStr("800922"));   
  383. "MB5(/"12/"):"+m.getMD5ofStr("781214"));   
  384. "MD5(/"13/"):"+m.getMD5ofStr("13"));   
  385. "MD5(/"message digest/"):"+m.getMD5ofStr("message digest"));   
  386. "MD5(/"abcdefghijklmnopqrstuvwxyz/"):"+   
  387. "abcdefghijklmnopqrstuvwxyz"));   
  388. "MD5(/"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/"):"+   
  389. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));   
  390.                 }   
  391. else
  392. "MD5(" + args[0] + ")=" + m.getMD5ofStr(args[0]));   
  393.                    
  394.             
  395.         }   
  396.     }