db2ab1b20e412a3489d70fbd2fad5c4920fe72d4.svn-base 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.yihu.utils;
  2. import java.security.*;
  3. /**
  4. * @author Houzq
  5. * @company yihu.com
  6. * 2013-4-8下午08:14:00
  7. * 加密类:加密完成后需要转换成16 进制表示的字符串
  8. */
  9. public class MD5 {
  10. private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
  11. "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
  12. /**
  13. * 转换字节数组16进制字串
  14. */
  15. public static String byteArrayToHexString(byte[] b) {
  16. StringBuffer resultSb = new StringBuffer();
  17. for (int i = 0; i < b.length; i++) {
  18. resultSb.append(byteToHexString(b[i]));
  19. }
  20. return resultSb.toString();
  21. }
  22. private static String byteToHexString(byte b) {
  23. int n = b;
  24. if (n < 0)
  25. n = 256 + n;
  26. int d1 = n / 16;
  27. int d2 = n % 16;
  28. return hexDigits[d1] + hexDigits[d2];
  29. }
  30. public static String compile(String origin) {
  31. String resultString = null;
  32. try {
  33. resultString = new String(origin);
  34. MessageDigest md = MessageDigest.getInstance("MD5");
  35. resultString = byteArrayToHexString(md.digest(resultString
  36. .getBytes()));
  37. } catch (Exception ex) {
  38. }
  39. return resultString;
  40. }
  41. }