0

I found this piece of code that returns the checksum for a given String.

public static String getChecksum(String md5) {
    int counter = 0;
    while (counter != 2) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuffer sb = new StringBuffer();

            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
                        .substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
            continue;
        }
    }
    return null;
}

I would now like to do the opposite- i.e. given the checksum, get the original String back. How is this possible?

user3254893
  • 1,071
  • 2
  • 13
  • 14
  • 2
    The whole point of hashing is to make that impossible. You must never be able to do that. On a related note, your hash is insecure; use scrypt or bcrypt or PBKDFv2. – SLaks Feb 03 '15 at 21:02

2 Answers2

1

You cannot. The point of a cryptographic hash function (or a "MessageDigest") is that it is one-way - there is no known method to reverse the hash, besides brute force (where you hash every possible password, and see which one gives you the same hash).

If you want to reverse the process, then you are not looking for a hash function.

user253751
  • 57,427
  • 7
  • 48
  • 90
0

A hash is a one-way cryptographic function, it has been purposefully and deliberately designed so that you can never, ever retrieve the plaintext given a hash.

You can play games using dictionaries etc. to try to guess what the hash was... but you can't "decrypt" it.

Little Code
  • 1,315
  • 2
  • 16
  • 37