Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decode md5 ecnrytion in java [duplicate]

public static String convertToMD5(String input) throws Exception {
    String md5 = null;
    if (null == input)
        return null;
    try {
        // Create MessageDigest object for MD5
        MessageDigest digest = MessageDigest.getInstance("MD5");
        // Update input string in message digest
        digest.update(input.getBytes(), 0, input.length());
        // Converts message digest value in base 16 (hex)
        md5 = new BigInteger(1, digest.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {

        throw e;
    }
    return md5;
}

using this code to encrypt the string i want to decode md5 encryption to normal string? can you help


2 Answers

md5 is not a encryption algorithm. It is a hash function. The hashed string can not be decoded. The original string is "destroyed" / hashed forever.

like image 105
dieter Avatar answered Jul 26 '26 17:07

dieter


To add onto @dit's answer, you only have one option; which is to compare MD5 strings, for example. MD5("cat") == MD5("cat"), there is no way to derive "cat" from MD5("cat") because as explained it's a hash function.

Here is something you can use for comparison:

public static boolean matching(String orig, String compare){
    String md5 = null;
    try{
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(compare.getBytes());
        byte[] digest = md.digest();
        md5 = new BigInteger(1, digest()).toString(16);

        return md5.equals(orig);

    } catch (NoSuchAlgorithmException e) {
        return false;
    }

    return false;
}

Then you can call matching("d077f244def8a70e5ea758bd8352fcd8", "cat"); which will return true, and if matching(MD5("x"), "y") it will return false.

like image 38
px06 Avatar answered Jul 26 '26 17:07

px06



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!