MD5编码工具类 MD5Code.java

  1. package com.util;
  2. /**
  3. * MD5编码工具类
  4. *
  5. */
  6. public class MD5Code {
  7. static final int S11 = 7;
  8. static final int S12 = 12;
  9. static final int S13 = 17;
  10. static final int S14 = 22;
  11. static final int S21 = 5;
  12. static final int S22 = 9;
  13. static final int S23 = 14;
  14. static final int S24 = 20;
  15. static final int S31 = 4;
  16. static final int S32 = 11;
  17. static final int S33 = 16;
  18. static final int S34 = 23;
  19. static final int S41 = 6;
  20. static final int S42 = 10;
  21. static final int S43 = 15;
  22. static final int S44 = 21;
  23. static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0 };
  27. private long[] state = new long[4];// state (ABCD)
  28. private long[] count = new long[2];// number of bits, modulo 2^64 (lsb
  29. // first)
  30. private byte[] buffer = new byte[64]; // input buffer
  31. public String digestHexStr;
  32. private byte[] digest = new byte[16];
  33. public String getMD5ofStr(String inbuf) {
  34. md5Init();
  35. md5Update(inbuf.getBytes(), inbuf.length());
  36. md5Final();
  37. digestHexStr = "";
  38. for (int i = 0; i < 16; i++) {
  39. digestHexStr += byteHEX(digest[i]);
  40. }
  41. return digestHexStr;
  42. }
  43. public MD5Code() {
  44. md5Init();
  45. return;
  46. }
  47. private void md5Init() {
  48. count[0] = 0L;
  49. count[1] = 0L;
  50. // /* Load magic initialization constants.
  51. state[0] = 0x67452301L;
  52. state[1] = 0xefcdab89L;
  53. state[2] = 0x98badcfeL;
  54. state[3] = 0x10325476L;
  55. return;
  56. }
  57. private long F(long x, long y, long z) {
  58. return (x & y) | ((~x) & z);
  59. }
  60. private long G(long x, long y, long z) {
  61. return (x & z) | (y & (~z));
  62. }
  63. private long H(long x, long y, long z) {
  64. return x ^ y ^ z;
  65. }
  66. private long I(long x, long y, long z) {
  67. return y ^ (x | (~z));
  68. }
  69. private long FF(long a, long b, long c, long d, long x, long s, long ac) {
  70. a += F(b, c, d) + x + ac;
  71. a = ((int) a << s) | ((int) a >>> (32 - s));
  72. a += b;
  73. return a;
  74. }
  75. private long GG(long a, long b, long c, long d, long x, long s, long ac) {
  76. a += G(b, c, d) + x + ac;
  77. a = ((int) a << s) | ((int) a >>> (32 - s));
  78. a += b;
  79. return a;
  80. }
  81. private long HH(long a, long b, long c, long d, long x, long s, long ac) {
  82. a += H(b, c, d) + x + ac;
  83. a = ((int) a << s) | ((int) a >>> (32 - s));
  84. a += b;
  85. return a;
  86. }
  87. private long II(long a, long b, long c, long d, long x, long s, long ac) {
  88. a += I(b, c, d) + x + ac;
  89. a = ((int) a << s) | ((int) a >>> (32 - s));
  90. a += b;
  91. return a;
  92. }
  93. private void md5Update(byte[] inbuf, int inputLen) {
  94. int i, index, partLen;
  95. byte[] block = new byte[64];
  96. index = (int) (count[0] >>> 3) & 0x3F;
  97. // /* Update number of bits */
  98. if ((count[0] += (inputLen << 3)) < (inputLen << 3))
  99. count[1]++;
  100. count[1] += (inputLen >>> 29);
  101. partLen = 64 - index;
  102. // Transform as many times as possible.
  103. if (inputLen >= partLen) {
  104. md5Memcpy(buffer, inbuf, index, 0, partLen);
  105. md5Transform(buffer);
  106. for (i = partLen; i + 63 < inputLen; i += 64) {
  107. md5Memcpy(block, inbuf, 0, i, 64);
  108. md5Transform(block);
  109. }
  110. index = 0;
  111. } else
  112. i = 0;
  113. // /* Buffer remaining input */
  114. md5Memcpy(buffer, inbuf, index, i, inputLen - i);
  115. }
  116. private void md5Final() {
  117. byte[] bits = new byte[8];
  118. int index, padLen;
  119. // /* Save number of bits */
  120. Encode(bits, count, 8);
  121. // /* Pad out to 56 mod 64.
  122. index = (int) (count[0] >>> 3) & 0x3f;
  123. padLen = (index < 56) ? (56 - index) : (120 - index);
  124. md5Update(PADDING, padLen);
  125. // /* Append length (before padding) */
  126. md5Update(bits, 8);
  127. // /* Store state in digest */
  128. Encode(digest, state, 16);
  129. }
  130. private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
  131. int len) {
  132. int i;
  133. for (i = 0; i < len; i++)
  134. output[outpos + i] = input[inpos + i];
  135. }
  136. private void md5Transform(byte block[]) {
  137. long a = state[0], b = state[1], c = state[2], d = state[3];
  138. long[] x = new long[16];
  139. Decode(x, block, 64);
  140. /* Round 1 */
  141. a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
  142. d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
  143. c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
  144. b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
  145. a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
  146. d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
  147. c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
  148. b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
  149. a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
  150. d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
  151. c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
  152. b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
  153. a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
  154. d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
  155. c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
  156. b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
  157. /* Round 2 */
  158. a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
  159. d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
  160. c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
  161. b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
  162. a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
  163. d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
  164. c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
  165. b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
  166. a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
  167. d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
  168. c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
  169. b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
  170. a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
  171. d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
  172. c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
  173. b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
  174. /* Round 3 */
  175. a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
  176. d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */
  177. c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
  178. b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
  179. a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
  180. d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
  181. c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
  182. b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
  183. a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
  184. d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
  185. c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
  186. b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
  187. a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
  188. d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
  189. c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
  190. b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
  191. /* Round 4 */
  192. a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
  193. d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
  194. c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
  195. b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
  196. a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
  197. d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
  198. c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
  199. b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
  200. a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
  201. d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
  202. c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
  203. b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
  204. a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
  205. d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
  206. c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
  207. b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
  208. state[0] += a;
  209. state[1] += b;
  210. state[2] += c;
  211. state[3] += d;
  212. }
  213. private void Encode(byte[] output, long[] input, int len) {
  214. int i, j;
  215. for (i = 0, j = 0; j < len; i++, j += 4) {
  216. output[j] = (byte) (input[i] & 0xffL);
  217. output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
  218. output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
  219. output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
  220. }
  221. }
  222. private void Decode(long[] output, byte[] input, int len) {
  223. int i, j;
  224. for (i = 0, j = 0; j < len; i++, j += 4)
  225. output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
  226. | (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
  227. return;
  228. }
  229. public static long b2iu(byte b) {
  230. return b < 0 ? b & 0x7F + 128 : b;
  231. }
  232. public static String byteHEX(byte ib) {
  233. char[] Digit = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘,
  234. ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ };
  235. char[] ob = new char[2];
  236. ob[0] = Digit[(ib >>> 4) & 0X0F];
  237. ob[1] = Digit[ib & 0X0F];
  238. String s = new String(ob);
  239. return s;
  240. }
  241. }
时间: 2024-08-29 08:46:19

MD5编码工具类 MD5Code.java的相关文章

MD5编码工具类

package util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Crypter { public static String md5Encrypt(String originalString){ StringBuffer sb = new StringBuffer(); //if((null != str) || (0 != (str.

java版MD5签名工具类

package com.net.util; import java.security.MessageDigest; /** * MD5签名工具类 * @author zhangdi * */ public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i+

java MD5数据加密工具类

package com.wetuo.util; import java.security.MessageDigest; /**  * 数据加密工具类  * @author wzp  *  */ public class DataUtil { public static String md5(String str) { StringBuffer buffer = new StringBuffer(); char[] chars = { '0', '1', '2', '3', '4', '5', '

java常用工具类(java技术交流群57388149)

package com.itjh.javaUtil; import java.util.ArrayList; import java.util.List; /** * * String工具类. <br> * * @author 宋立君 * @date 2014年06月24日 */ public class StringUtil { private static final int INDEX_NOT_FOUND = -1; private static final String EMPTY =

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ?Copyright 蕃薯耀 2017年9月13日 http://www.cnblogs.com/fanshuyao/ 直接上代码: import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.ref

android开发MD5加密工具类(一)

MD5加密工具类整理: 1 package com.gzcivil.utils; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 7 public class MD5Tool { 8 9 public static String md5(String string) {

wemall app商城源码android开发MD5加密工具类

wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享android开发MD5加密工具类主要代码,供技术员参考学习. package com.gzcivil.utils; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgori

md5加密工具类

md5加密工具类: package cn.sniper.encrypt.util; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /**  * 加密工具类  *   * md5加密出来的长度是32位  *   * sha加密出来的长度是40位  *   * @author sniper  

Java字符串转16 进制工具类Hex.java

原文:Java字符串转16 进制工具类Hex.java 源代码下载地址:http://www.zuidaima.com/share/1550463378410496.htm Java 字符串转 16 进制工具类 Hex.java 实现 16进制 0xfecd .. 和 java 字符串之间的互转换! 如果做开发,通常用户登陆密码都会 mad5(salt + pwd) 然后再将 md 之后的数据 hex 一下. 这个工具类,就是实现此效果的. /* * */ package com.zuidaim