Md5Utils.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.xc.utils;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. public class Md5Utils {
  5. public static String getMD5(String str) throws Exception {
  6. try {
  7. MessageDigest md = MessageDigest.getInstance("MD5");
  8. md.update(str.getBytes());
  9. return (new BigInteger(1, md.digest())).toString(16);
  10. } catch (Exception e) {
  11. throw new Exception();
  12. }
  13. }
  14. public static String md5_32(String plainText) throws Exception {
  15. String re_md5 = new String();
  16. try {
  17. MessageDigest md = MessageDigest.getInstance("MD5");
  18. md.update(plainText.getBytes());
  19. byte[] b = md.digest();
  20. StringBuffer buf = new StringBuffer("");
  21. for (int offset = 0; offset < b.length; offset++) {
  22. int i = b[offset];
  23. if (i < 0)
  24. i += 256;
  25. if (i < 16)
  26. buf.append("0");
  27. buf.append(Integer.toHexString(i));
  28. }
  29. re_md5 = buf.toString();
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. return re_md5;
  34. }
  35. public static void main(String[] args) throws Exception {
  36. System.out.println(md5_32("SLPNXGC157080458540512750000http://serverback.comhttp://serverback.comea40b08d39a043b882ab197c6c9c7699"));
  37. }
  38. }