-16

I have MD5-hashed a string.

def hash(s: String) = {
    val m = java.security.MessageDigest.getInstance("MD5")
    val b = s.getBytes("UTF-8")
    m.update(b, 0, b.length)
    new java.math.BigInteger(1, m.digest()).toString(16)
}

Now I want the original string back. How can I do this?

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
  • 3
    You can't. It's a one way function, not intended to be reversible. – Kayaman Nov 21 '14 at 11:30
  • 6
    Well, MD5 is more or less considered broken by now. It's likely that we will see significant advances in breaking MD5 in the next couple of years, so, if you can wait 10 years, then it should be possible with a combination of advances in cryptanalysis and computing power to do this in a practical timeframe, even without the use of rainbow tables. – Jörg W Mittag Nov 21 '14 at 12:10
  • 1
    You didn't supply any password when "encrypting" it, so it wouldn't be a useful form of encryption even if it were encryption, since anyone could run the decryption function. – Boann Nov 21 '14 at 16:35
  • 3
    @Jörg: No, the hash does not contain enough information about the original string. It isn't a matter of complexity. – Chris Martin Nov 21 '14 at 17:50

3 Answers3

5

MD5 is a cryptographic hash function. Cryptographic hashes are one way functions. You cannot reverse a cryptographic hash value, but you can brute force messages to find one.

Brute forcing means trying all possible input strings and then checking if the hash value is correct. This is possible because cryptographic hashes are also computationally unique. This means that there are endless messages that will result in the same hash value being generated, but it is impossible to two resulting in the same hash. As MD5 is broken, MD5 hashes are not unique for specially constructed messages. It is called a collision if you can find two messages that have the same hash value.

It is also possible to create huge lookup tables called rainbow tables. This can help speedup looking for the right input. That only works for relatively small or guessable data input; i.e. they are mostly used to find weak passwords. Some of these databases can be found online.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
2

You can not decrypt MD5 and this is feature of MD5, if you want encrypt/decrypt data then use other encryption techniques like AES

1

You couldn't. With MD5 being one-way hash function, it is not possible.