简体   繁体   中英

Java MessageDigest MD5 Not Returning Expected Outcome

I got extremely bored so I'm making an experimental Brute Forcer. I'm having some issues with MD5 however. I'm getting two very different outputs,

1aabac6d068eef6a7bad3fdf50a05cc8
-7d881f6ef28afe6a4bb78689e91f6e53

The first one is valid and is dd , the second one is invalid, even if I remove the leading hyphen.

I was looking at this answer and I adjusted what I had to that which solved my primary issue however I'm still getting invalid MD5s.

My code:

public boolean testValidity(String s) {
    try {
        MessageDigest md = MessageDigest.getInstance(name());
        byte[] hashDigest = md.digest(s.getBytes("UTF-8"));
        String hash = String.format("%032x", new BigInteger(md.digest(s.getBytes("UTF-8"))));
        System.out.println(hash);
        return getCompare().equalsIgnoreCase(hash);
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NullPointerException e) {
        e.printStackTrace();
        return false;
    }
}

In the above, name() just gets "MD5" and getCompare() gets a hash to compare it against.

My question is, how can I fix my code to make sure all the MD5s are valid?

Your BigInteger constructor uses twos complement. Use the sign/magnitude variant instead with first parameter 1. http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(int,%20byte[])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM