package com.weather.Util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Cracker {
    public static void main(String[] args) {

        // 要解密的MD5散列值
        String md5Hash = "0192023a7bbd73250516f069df18b500";

        for (int i = 0; i < 1000000; i++) {
            // 生成6位数字密码尝试
            String attempt = String.format("%06d", i);

            // 获取尝试的MD5散列值
            String attemptHash = getMD5Hash(attempt);

            if (attemptHash.equals(md5Hash)) {
                System.out.println("解密成功：密码为 " + attempt);
                return;
            }
        }

        System.out.println("解密失败：未找到匹配的密码");
    }

    private static String getMD5Hash(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger no = new BigInteger(1, messageDigest);
            String hashText = no.toString(16);

            while (hashText.length() < 32) {
                hashText = "0" + hashText;
            }

            return hashText;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}
