java实现

  1. package com.xinyan.credit.util;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.security.SecureRandom;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Arrays;
  8. import java.util.Date;
  9. import sun.misc.BASE64Encoder;
  10. public class MD5Utils {
  11. private static final String HEX_NUMS_STR = "0123456789ABCDEF";
  12. private static final Integer SALT_LENGTH = Integer.valueOf(12);
  13. private static final String KEY_MD5 = "MD5";
  14. private static final String[] STR_DIGITS = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
  15. "e", "f" };
  16. public static byte[] hexStringToByte(String hex) {
  17. int len = hex.length() / 2;
  18. byte[] result = new byte[len];
  19. char[] hexChars = hex.toCharArray();
  20. for (int i = 0; i < len; i++) {
  21. int pos = i * 2;
  22. result[i] = (byte) ("0123456789ABCDEF".indexOf(hexChars[pos]) << 4
  23. | "0123456789ABCDEF".indexOf(hexChars[(pos + 1)]));
  24. }
  25. return result;
  26. }
  27. public static String byteToHexString(byte[] b) {
  28. StringBuffer hexString = new StringBuffer();
  29. for (int i = 0; i < b.length; i++) {
  30. String hex = Integer.toHexString(b[i] & 0xFF);
  31. if (hex.length() == 1) {
  32. hex = new StringBuilder().append('0').append(hex).toString();
  33. }
  34. hexString.append(hex.toUpperCase());
  35. }
  36. return hexString.toString();
  37. }
  38. public static boolean validPassword(String password, String passwordInDb)
  39. throws NoSuchAlgorithmException, UnsupportedEncodingException {
  40. byte[] pwdInDb = hexStringToByte(passwordInDb);
  41. byte[] salt = new byte[SALT_LENGTH.intValue()];
  42. System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH.intValue());
  43. MessageDigest md = MessageDigest.getInstance("MD5");
  44. md.update(salt);
  45. md.update(password.getBytes("UTF-8"));
  46. byte[] digest = md.digest();
  47. byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH.intValue()];
  48. System.arraycopy(pwdInDb, SALT_LENGTH.intValue(), digestInDb, 0, digestInDb.length);
  49. return Arrays.equals(digest, digestInDb);
  50. }
  51. public static String getEncryptedPwd(String password)
  52. throws NoSuchAlgorithmException, UnsupportedEncodingException {
  53. byte[] pwd = null;
  54. SecureRandom random = new SecureRandom();
  55. byte[] salt = new byte[SALT_LENGTH.intValue()];
  56. random.nextBytes(salt);
  57. MessageDigest md = null;
  58. md = MessageDigest.getInstance("MD5");
  59. md.update(salt);
  60. md.update(password.getBytes("UTF-8"));
  61. byte[] digest = md.digest();
  62. pwd = new byte[digest.length + SALT_LENGTH.intValue()];
  63. System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH.intValue());
  64. System.arraycopy(digest, 0, pwd, SALT_LENGTH.intValue(), digest.length);
  65. return byteToHexString(pwd);
  66. }
  67. public static String encryptMD5(byte[] data) {
  68. try {
  69. MessageDigest md5 = MessageDigest.getInstance("MD5");
  70. md5.update(data);
  71. return byteToString(md5.digest());
  72. } catch (Exception e) {
  73. }
  74. return "";
  75. }
  76. private static String byteToArrayString(byte bByte) {
  77. int iRet = bByte;
  78. if (iRet < 0) {
  79. iRet += 256;
  80. }
  81. int iD1 = iRet / 16;
  82. int iD2 = iRet % 16;
  83. return new StringBuilder().append(STR_DIGITS[iD1]).append(STR_DIGITS[iD2]).toString();
  84. }
  85. private static String byteToString(byte[] bByte) {
  86. StringBuilder sb = new StringBuilder();
  87. for (int i = 0; i < bByte.length; i++) {
  88. sb.append(byteToArrayString(bByte[i]));
  89. }
  90. return sb.toString();
  91. }
  92. public static String encryptMD5(String inputText) {
  93. return encrypt(inputText, "MD5");
  94. }
  95. private static String encrypt(String inputText, String algorithmName) {
  96. if ((inputText == null) || ("".equals(inputText.trim()))) {
  97. return "";
  98. }
  99. if ((algorithmName == null) || ("".equals(algorithmName.trim())))
  100. algorithmName = "md5";
  101. try {
  102. MessageDigest m = MessageDigest.getInstance(algorithmName);
  103. m.update(inputText.getBytes("UTF-8"));
  104. byte[] s = m.digest();
  105. return hex(s);
  106. } catch (NoSuchAlgorithmException e) {
  107. error("MD5加密异常:" + e);
  108. return null;
  109. } catch (UnsupportedEncodingException e) {
  110. error("MD5加密异常:{}" + e);
  111. }
  112. return null;
  113. }
  114. private static String hex(byte[] arr) {
  115. StringBuffer sb = new StringBuffer();
  116. for (int i = 0; i < arr.length; i++) {
  117. sb.append(Integer.toHexString(arr[i] & 0xFF | 0x100).substring(1, 3));
  118. }
  119. return sb.toString();
  120. }
  121. public static String ecodeByMD5(String originstr) {
  122. String result = null;
  123. char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  124. if (originstr != null) {
  125. try {
  126. MessageDigest md = MessageDigest.getInstance("MD5");
  127. byte[] source = originstr.getBytes("utf-8");
  128. md.update(source);
  129. byte[] tmp = md.digest();
  130. char[] str = new char[32];
  131. int i = 0;
  132. for (int j = 0; i < 16; i++) {
  133. byte b = tmp[i];
  134. str[(j++)] = hexDigits[(b >>> 4 & 0xF)];
  135. str[(j++)] = hexDigits[(b & 0xF)];
  136. }
  137. result = new String(str);
  138. } catch (NoSuchAlgorithmException e) {
  139. return null;
  140. } catch (UnsupportedEncodingException e) {
  141. return null;
  142. }
  143. }
  144. return result;
  145. }
  146. public static String encryptMd5(String originStr) {
  147. String result = null;
  148. if (null != originStr) {
  149. try {
  150. MessageDigest md = MessageDigest.getInstance("md5");
  151. byte[] mess = originStr.getBytes("utf-8");
  152. md.reset();
  153. byte[] hash = md.digest(mess);
  154. BASE64Encoder encoder = new BASE64Encoder();
  155. result = encoder.encode(hash);
  156. } catch (Exception e) {
  157. error("encryptMd5 error:" + e);
  158. }
  159. }
  160. return result;
  161. }
  162. public static void error(String msg) {
  163. System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\t: " + msg);
  164. }
  165. public static void main(String[] args) {
  166. String idno = MD5Utils.encryptMD5("110102200101017072");//1b0b0dbf7650414b7117fcaadce9ddad
  167. System.out.println(idno);
  168. String name = MD5Utils.encryptMD5("李四");//36c942351ec9cc3ad124e288a5c9cf0b
  169. System.out.println(name);
  170. }
  171. }
文档更新时间: 2019-06-17 17:50   作者:support