安全-md5

小蝸牛也有夢想發表於2020-10-09

工具類

import org.apache.commons.codec.binary.Hex;

import java.security.MessageDigest;
import java.util.Random;


public class MD5Util {

    public static String md32(String src) {
        try {
            byte[] bs = MessageDigest.getInstance("MD5").digest(src.getBytes());
            return new String(new Hex().encode(bs));
        } catch (Exception e) {
            return null;
        }
    }

    public static String md16(String str) {
        return md32(str).substring(8, 24);
    }

    public static String md32Salt(String input) {
        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();

        input = md32(input + salt);
        char[] cs = new char[48];
        for (int i = 0; i < 48; i += 3) {
            cs[i] = input.charAt(i / 3 * 2);
            char c = salt.charAt(i / 3);
            cs[i + 1] = c;
            cs[i + 2] = input.charAt(i / 3 * 2 + 1);
        }
        return new String(cs);
    }

    public static boolean verify(String input, 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 md32(input + salt).equals(new String(cs1));
    }
}

破解網站

https://www.cmd5.com/

https://pmd5.com/

破解工具

https://hashcat.net/hashcat/
https://github.com/hashcat/hashcat