老宋的地盘

 找回密码
 我要加入
搜索
老宋 首页 文章 技术 查看内容

MD5盐值加密

2023-7-3 13:38| 发布者: herosrx| 查看: 211| 评论: 0

摘要: 突然在网上发现,在md5的加解密站长工具中不是所有的md5加密值都是可以解密的,仔细看了一下,为了保证数据的更高的安全性,还有一种md5盐值加密的方法。1.普通MD5加密public class MD5Test1 { public static Strin ...
突然在网上发现,在md5的加解密站长工具中不是所有的md5加密值都是可以解密的,仔细看了一下,为了保证数据的更高的安全性,还有一种md5盐值加密的方法。

1.普通MD5加密

public class MD5Test1 {
public static String MD5(String input) {  
        MessageDigest md5 = null;  
        try {  
            md5 = MessageDigest.getInstance("MD5");  
        } catch (NoSuchAlgorithmException e) {  
            return "check jdk";  
        } catch (Exception e) {  
            e.printStackTrace();  
            return "";  
        }  
        char[] charArray = input.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
  
        for (int i = 0; i < charArray.length; i++)  
            byteArray[i] = (byte) charArray[i];  
        byte[] md5Bytes = md5.digest(byteArray);  
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++) {  
            int val = ((int) md5Bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
  
    } 
public static void main(String[] args) {
MD5Test1 md = new MD5Test1();
System.out.println(md.MD5("123456"));
}
}

加密后生成的字符串如下:



然后问题就来了,直接拿着这个32位的字符串去站长工具上面去解密,秒解。。。细思极恐。。。看了很多的解释,基本上是说MD5进行的常规加密生成的字符串保存到相应的数据库,结果你来调的时候别人直接拿现成的就可以了。

2.MD5盐值加密。

 原理是通过生成随机数与MD5生成字符串进行组合,这样的随机性和不确定性就算是再怎么厉害的站长工具都解不开。

public class MD5Test2 {
  public static String generate(String password) {  
          Random r = new Random();  
          StringBuilder sb = new StringBuilder(16);  
          sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));  
          int len = sb.length();  
          if (len < 16) {  
              for (int i = 0; i < 16 - len; i++) {  
                  sb.append("0");  
              }  
          }  
          String salt = sb.toString();  
          password = md5Hex(password + salt);  
          char[] cs = new char[48];  
          for (int i = 0; i < 48; i += 3) {  
              cs[i] = password.charAt(i / 3 * 2);  
              char c = salt.charAt(i / 3);  
              cs[i + 1] = c;  
              cs[i + 2] = password.charAt(i / 3 * 2 + 1);  
          }  
          return new String(cs);  
      }  
  /**  
       * 获取十六进制字符串形式的MD5摘要  
       */  
      private static String md5Hex(String src) {  
          try {  
              MessageDigest md5 = MessageDigest.getInstance("MD5");  
              byte[] bs = md5.digest(src.getBytes());  
              return new String(new Hex().encode(bs));  
          } catch (Exception e) {  
              return null;  
          }  
      } 
      public static boolean verify(String password, String md5) {  
          char[] cs1 = new char[32];  
          char[] cs2 = new char[16];  
          for (int i = 0; i < 48; i += 3) {  
              cs1[i / 3 * 2] = md5.charAt(i);  
              cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);  
              cs2[i / 3] = md5.charAt(i + 1);  
          }  
          String salt = new String(cs2);  
          return md5Hex(password + salt).equals(new String(cs1));  
      }  
      
      public static void main(String[] args) {
        String ss = MD5Test2.generate("123456");
    System.out.println(MD5Test2.generate("123456"));
    System.out.println("是否是同一字符串:"+MD5Test2.verify("123456",ss));
}
}

执行main方法后的操作:

首先入眼一看就觉察到不同,总共生成了32+18共50位的字符串,在代码中定义了18位随机数与md5加密字符串的拼接,这个时候你再去站长工具,站长工具会告诉你我们解不开。。因为加入随机数,代表了他的不确定性。可以这么说,自己本机每次运行都会生成不同的加密字符串,也就更好的保证了数据的安全。

其实加密的方式方法有很多种,还需要我们不断的去学习。


路过

雷人

握手

鲜花

鸡蛋

相关阅读

最新评论

歌名 - 歌手
0:00

    QQ|手机版|小黑屋|老宋 ( 备案中... )

    GMT+8, 2024-4-27 16:11 , Processed in 0.124073 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    返回顶部