-1

I'm using the following method to generate hash. $hash is a mixture of ip and key.

$key = "my_key";
$pieces = explode(".", $remoteAddr);
$ip = $pieces[0].".".$pieces[1].".".$pieces[2];
$hash = md5($ip.$key);

I have the hash and the key, How can I decode the hash to discover the ip address?

Bnhjhvbq7
  • 169
  • 3
  • 13
  • http://stackoverflow.com/questions/1240852/is-it-possible-to-decrypt-md5-hashes – Neysor Nov 22 '13 at 21:09
  • 4
    Hashes are one-way. They cannot be "decoded". – Jon Nov 22 '13 at 21:09
  • Hashes are a one-way street. You can't. – Dave Nov 22 '13 at 21:10
  • but I have the key and the hash itself, I think there should be an option. – Bnhjhvbq7 Nov 22 '13 at 21:10
  • There is an option: [Is it possible to decrypt md5 hashes?](http://stackoverflow.com/q/1240852) but using that makes zero sense. Why are you building this hash in the first place, what are you doing with it? – Pekka Nov 22 '13 at 21:11
  • 3
    @bnhjhvbq7 - you can think as much as you like, but mathematics doesn't always provide the options that you want.... decryption simply isn't a possibility, though your may be able to brute-force it... but even then, you may simply have found a different IP address that hashes to the same value – Mark Baker Nov 22 '13 at 21:15
  • @Bnhjhvbq7: A hash could be of a password, a zip file, the entire contents of your hard drive, or anything else. We're talking like 2^trillions possible sequences of bytes -- and each hash will end up (in MD5's case) as a 128-bit number. There are way, *way* too many sequences for a 128-bit number to uniquely identify each one. Google "pigeonhole principle" for a bit more info. – cHao Nov 22 '13 at 21:18

3 Answers3

3

You don't have a key, you have a salt, which is just extra data concatenated onto what you hashed in the first place.

Hashes are one-way functions. They cannot be decoded.

You can prove this to yourself easily. Think of a large file... gigabytes in size. If you were to pass that into a hash function, like MD5, you're still going to get your 32-character hash. Now, if we could compress data in this method and undo it, the world's storage problems would be solved! It isn't possible.

The other reason hashes are one-way is that collisions occur. For any given hash, there are many (infinite?) ways to get that same hash with different data.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Wait so you can theoretically log in into account with different password(In case it generated same hash) if login.php does if(md5(password) === dbPasswordHash) ? – Nick Apr 24 '20 at 09:14
  • @Nick Sure! Good luck finding a collision though. The whole point of the function is that finding collisions is extremely difficult. Also, these days, don't use MD5... weaknesses have been found which make it possible to find collisions more easily than expected. Try one of the SHA-2 variants. – Brad Apr 24 '20 at 13:12
  • Thanks for info! – Nick Apr 24 '20 at 15:48
3

There are several different types of encryption:

Symmetrical: You can go back and forth if you know the algorithm and the key. i.e: Base64

Asymmetric: Public and Private keys, private is used to encrypt and public to decrypt. Therefore, you can only go back and forth if you have the proper keys. i.e: RSA

Hashing: One way encryption. It is extremely difficult (depends on the algorithm) to go back or recover a message once it has been hashed. i.e: MD5, SHA1, SHA256

You may want to change the way you are encrypting stuff if you want to recover the original message.

fos.alex
  • 5,317
  • 4
  • 16
  • 18
3

A [cryptographic] hash function is a one-way operation - that is, it is not a bijective function and is subject to the Pigeonhole principle.

That being said, given a restricted domain, a collision (should one exist) can be discovered by generating a hash for each value in the domain and comparing it with the expected hash. This is known as brute-force attack.

In this case the input domain looks to be limited to "a.b.c" (3 of the 4 octets of an IPv4 address), where a, b, and c are values in the range 0..255: thus there is an upper-bound of 256^3 ~ 2^24 ~ 16 million inputs to check1.

Again, this is not reversing the hash, but rather finding some input - which might not be the original value! - that results in a particular hash value.


1 Modern CPUs - not to mention GPUs and specialized hash hardware - don't even blink at 16 million hash generations. This is why password hashing must use a distinct salt (to avoid rainbow tables) and a slow hash function such as bcrypt or scrypt.

user2864740
  • 60,010
  • 15
  • 145
  • 220