当前位置:   article > 正文

安全编程之MD5撒盐加密_md5加盐加密代码

md5加盐加密代码

MD5撒盐加密 主要可以做字符串验证--最合适的是登录密码验证操作

  1. import java.security.MessageDigest;
  2. import java.util.Random;
  3. public class MD5Util {
  4. /**
  5. * 加盐MD5
  6. *
  7. * @param password
  8. * @return
  9. * @author daniel
  10. * @time 2016-6-11 下午8:45:04
  11. */
  12. public static String generateMD5(String password) {
  13. Random r = new Random();
  14. StringBuilder sb = new StringBuilder(16);
  15. sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
  16. int len = sb.length();
  17. if (len < 16) {
  18. for (int i = 0; i < 16 - len; i++) {
  19. sb.append("0");
  20. }
  21. }
  22. String salt = sb.toString();
  23. password = md5Hex(password + salt);
  24. char[] cs = new char[48];
  25. for (int i = 0; i < 48; i += 3) {
  26. cs[i] = password.charAt(i / 3 * 2);
  27. char c = salt.charAt(i / 3);
  28. cs[i + 1] = c;
  29. cs[i + 2] = password.charAt(i / 3 * 2 + 1);
  30. }
  31. return new String(cs);
  32. }
  33. /**
  34. * 校验加盐后是否和原文一致
  35. *
  36. * @param str
  37. * @param md5
  38. * @return
  39. * @author daniel
  40. * @time 2016-6-11 下午8:45:39
  41. */
  42. public static boolean verify(String str, String md5) {
  43. char[] cs1 = new char[32];
  44. char[] cs2 = new char[16];
  45. for (int i = 0; i < 48; i += 3) {
  46. cs1[i / 3 * 2] = md5.charAt(i);
  47. cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
  48. cs2[i / 3] = md5.charAt(i + 1);
  49. }
  50. String salt = new String(cs2);
  51. return md5Hex(str + salt).equals(new String(cs1));
  52. }
  53. /**
  54. * 获取十六进制字符串形式的MD5摘要
  55. */
  56. private static String md5Hex(String src) {
  57. try {
  58. MessageDigest md5 = MessageDigest.getInstance("MD5");
  59. byte[] bs = md5.digest(src.getBytes());
  60. return new String(hexEncode(bs));
  61. } catch (Exception e) {
  62. return null;
  63. }
  64. }
  65. /**
  66. * 字节流转成十六进制表示
  67. */
  68. public static String hexEncode(byte[] src) {
  69. String strHex;
  70. StringBuilder sb = new StringBuilder();
  71. for (int n = 0; n < src.length; n++) {
  72. strHex = Integer.toHexString(src[n] & 0xFF);
  73. sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示,位数不够,高位补0
  74. }
  75. return sb.toString().trim();
  76. }
  77. // 测试主函数
  78. public static void main(String args[]) {
  79. // 原文
  80. String passwordStr = "password22";
  81. // 获取加盐后的MD5值
  82. String md5Value = MD5Util.generateMD5(passwordStr);
  83. System.out.println("加盐后MD5:" + md5Value);
  84. System.out.println("是否是同一字符串:" + MD5Util.verify(passwordStr,md5Value));
  85. System.out.println("-----------------------------------------------------------");
  86. //加盐后每次生成的MD5值
  87. String[] strMd5s = new String[]{"98641096ee18e91476d93a9de23915862536715a9740e702","554c50735a9c149b0a556a5d89302d794b4242c44257dd99"
  88. ,"69aa44d7f53f345e6ec5ef8eb6923eb3083890591a701b0d","36c28d591946a4928bc16878c9d29e20ba02b7bd5f80243f"};
  89. for (String strMd5:strMd5s){
  90. System.out.println("是否是同一字符串:" + MD5Util.verify(passwordStr, strMd5));
  91. }
  92. }
  93. }

运行结果:

加盐后MD5:09a233f6b90d583641b5531983e310394a49896d0564f367
是否是同一字符串:true
-----------------------------------------------------------
是否是同一字符串:false
是否是同一字符串:false
是否是同一字符串:true
是否是同一字符串:false

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/636269
推荐阅读
相关标签
  

闽ICP备14008679号