MD5加密加盐测试

2019-01-07 11:19:07
  1. import java.security.MessageDigest;
  2. import java.security.NoSuchAlgorithmException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6. /**
  7. * MD5加密加盐测试
  8. *
  9. * @author wangjing
  10. * @version 1.0 created 2018/10/16
  11. */
  12. public class MD5WithSalt {
  13. /**
  14. * 加盐库
  15. */
  16. private static final char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  17. private static final int registered_type = 0;
  18. private static final int login_type = 1;
  19. private static final String algorithm_md5 = "MD5";
  20. public static void main(String[] args) {
  21. String password = "123456";
  22. System.out.println("不加盐密文:" + MD5WithoutSalt(password));
  23. System.out.println("带盐密文:" + MD5WithSalt(password, 1));
  24. }
  25. /**
  26. * MD5不加盐加密
  27. *
  28. * @param inputStr
  29. * @return
  30. * @author wangjing
  31. * @date: 上午10:45 2018/10/25
  32. */
  33. public static String MD5WithoutSalt(String inputStr) {
  34. try {
  35. MessageDigest messageDigest = MessageDigest.getInstance(algorithm_md5);
  36. return byte2HexStr((messageDigest.digest(inputStr.getBytes())));
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. return e.getMessage();
  40. }
  41. }
  42. /**
  43. * MD5加盐加密
  44. *
  45. * @param inputStr
  46. * @param type (type是处理类型,0表示注册存hash值到库时,1表示登录验证时)
  47. * @return
  48. * @author wangjing
  49. * @date: 上午11:28 2018/10/25
  50. */
  51. public static String MD5WithSalt(String inputStr, int type) {
  52. try {
  53. MessageDigest messageDigest = MessageDigest.getInstance(algorithm_md5);
  54. String salt = null;
  55. //注册存hash值到库时,new salt
  56. if (registered_type == type) {
  57. salt = salt();
  58. //登录验证时,使用从库中查找到的hash值提取出的salt
  59. } else if (login_type == type) {
  60. //从库中查找到的hash值
  61. String queriedHash = "17624DBBB13CF4D4FEC0B6C6E296BA66A55B95A826AEB55C";
  62. salt = getSaltFromHash(queriedHash);
  63. }
  64. System.out.println("盐:" + salt);
  65. //加盐,输入加盐
  66. String inputWithSalt = inputStr + salt;
  67. String hashResult = byte2HexStr(messageDigest.digest(inputWithSalt.getBytes()));
  68. System.out.println("加盐密文:" + hashResult);
  69. /*将salt存储到hash值中,用于登陆验证密码时使用相同的盐*/
  70. char[] cs = new char[48];
  71. for (int i = 0; i < 48; i += 3) {
  72. cs[i] = hashResult.charAt(i / 3 * 2);
  73. //输出带盐,存储盐到hash值中;每两个hash字符中间插入一个盐字符
  74. cs[i + 1] = salt.charAt(i / 3);
  75. cs[i + 2] = hashResult.charAt(i / 3 * 2 + 1);
  76. }
  77. return new String(cs);
  78. } catch (NoSuchAlgorithmException e) {
  79. e.printStackTrace();
  80. return e.getMessage();
  81. }
  82. }
  83. /**
  84. * 将字节数组转成十六进制字符串
  85. *
  86. * @param bytes
  87. * @return
  88. * @author wangjing
  89. * @date: 下午9:22 2018/10/16
  90. */
  91. public static String byte2HexStr(byte[] bytes) {
  92. int len = bytes.length;
  93. StringBuffer stringBuffer = new StringBuffer();
  94. for (int i = 0; i < len; i++) {
  95. byte byte0 = bytes[i];
  96. stringBuffer.append(hex[byte0 >>> 4 & 0xf]);
  97. stringBuffer.append(hex[byte0 & 0xf]);
  98. }
  99. return stringBuffer.toString();
  100. }
  101. /**
  102. * 获取盐
  103. *
  104. * @return
  105. * @author wangjing
  106. * @date: 下午3:59 2018/10/23
  107. */
  108. public static String salt() {
  109. Random random = new Random();
  110. StringBuffer buffer = new StringBuffer(16);
  111. for (int i = 0; i < buffer.capacity(); i++) {
  112. buffer.append(hex[random.nextInt(16)]);
  113. }
  114. return buffer.toString();
  115. }
  116. /**
  117. * 从密文中提取盐
  118. *
  119. * @param hash
  120. * @return
  121. * @author wangjing
  122. * @date: 下午5:38 2018/10/26
  123. */
  124. public static String getSaltFromHash(String hash) {
  125. StringBuffer stringBuffer = new StringBuffer(16);
  126. char[] charArray = hash.toCharArray();
  127. for (int i = 0; i < hash.length(); i += 3) {
  128. stringBuffer.append(charArray[i + 1]);
  129. }
  130. return stringBuffer.toString();
  131. }
  132. }

0
1
0

添加评论

正在回复:
取消
0
0
1
0