JAVA LEARNER JAVA LEARNER - 26 days ago 16
Java Question

decode md5 ecnrytion in java

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

Answer

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.

Comments