76

I am using code $enrypt=md5($pass) and inserting $encrypt to database. I want to find out a way to decrypt them. I tried using a decrypting software but it says the hash should be of exactly 16 bytes. is there any way to decrypt it or to make it a 16 byte md5 hash?

My hash looks like this: c4ca4238a0b923820dcc

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Tomer
  • 801
  • 1
  • 6
  • 5
  • 17
    You don't decrypt MD5... – sachleen Mar 04 '13 at 04:42
  • 8
    Hashing is a one-way operation, meaning it cannot be decrypted. However, you could brute-force the hash to find what input(s) work for it. – FThompson Mar 04 '13 at 04:42
  • 4
    md5 is an old and easily breakable password hashing mechanism, I suggest you use the latest password encryption algorithm. – KyelJmD Mar 04 '13 at 04:42
  • 1
    md5 encrypt make some string lost. so you cannot decrypt it. – novalagung Mar 04 '13 at 04:45
  • 8
    I don't see the reason why this question is downvoted. the poster is probably a student. – KyelJmD Mar 04 '13 at 04:48
  • 6
    md5 is **not** an encryption function, as such it cannot be decrypted (since no actual encryption took place). It's a hashing function. – ccKep Mar 04 '13 at 04:58
  • 2
    Also: PHPs md5() function is supposed to return a 32-character hexadecimal number. Yours is 20 characters long. – ccKep Mar 04 '13 at 05:03

6 Answers6

78

As already stated, you cannot decrypt MD5 without attempting something like brute force hacking which is extremely resource intensive, not practical, and unethical.

However you could use something like this to encrypt / decrypt passwords/etc safely:

$input = "SmackFactory";

$encrypted = encryptIt( $input );
$decrypted = decryptIt( $encrypted );

echo $encrypted . '<br />' . $decrypted;

function encryptIt( $q ) {
    $cryptKey  = 'qJB0rGtIn5UB1xG03efyCp';
    $qEncoded      = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
    return( $qEncoded );
}

function decryptIt( $q ) {
    $cryptKey  = 'qJB0rGtIn5UB1xG03efyCp';
    $qDecoded      = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "\0");
    return( $qDecoded );
}

Using a encypted method with a salt would be even safer, but this would be a good next step past just using a MD5 hash.

BIT CHEETAH
  • 1,200
  • 1
  • 7
  • 10
  • 1
    For some the inputs it is not decrypting properly, Please help – Sukhwinder Sodhi Jan 04 '18 at 15:08
  • 3
    mcrypt_encrypt is already deprecated in PHP 7.1.0. Relying on this function is highly discouraged. – girish Feb 27 '18 at 11:02
  • @girish do you have any suggestion on how to achieve a similar result with php7? - I was going to use it, and the information I would encrypt are not important at all: I just want to shrink a long $input (which is a path) and don't want the user to read this path, mostly because its length. – aPugLife Mar 01 '18 at 16:02
  • Not sure. but there is no way to decrypt a hashed password in PHP. Maybe you have to do base64 encode and decode with string reverse and maybe a SALT key of your choice. – girish Mar 03 '18 at 19:20
  • Is this answer still updated? It's 2019 now and md5 is quite low processing power. Can't someone brute force it in a reasonable time? – Freedo Oct 09 '19 at 07:06
19

There is no way to decrypt MD5. Well, there is, but no reasonable way to do it. That's kind of the point.

To check if someone is entering the correct password, you need to MD5 whatever the user entered, and see if it matches what you have in the database.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 3
    No, there is no way to decrypt MD5, because MD5 is not a cipher. But as you probably meant its reversibility, you can only remember input+output pairs for a later lookup or try to find collisions. But you cannot ‘decrypt’ it. – Gumbo Mar 04 '13 at 04:59
  • @Gumbo: how do the MD5 decryptors work? It doesn't matter whether they store the key value strings, the process suggests that MD5 doen't use a custom salt which means it can be reversed if you get to the core of it. – Fr0zenFyr Jun 14 '13 at 04:52
  • 13
    Here's an oversimplified way of putting it: 5+5 is 10, but if you just have 10 you can't know that the original numbers were 5 and 5. However, if you know the algorithm (take two numbers and add them), then you can reverse the 10 to get 3+7 and it will be accepted because the result is the same. – Niet the Dark Absol Jun 14 '13 at 04:55
  • @Fr0zenFyr They are just huge lookup tables: Any known hash is mapped onto the input that was used to generate it; if someone looks up a hash that is known, you’ll get the input value. – Gumbo Jun 14 '13 at 05:21
  • @Kolink It’s not that easy. [Cryptographic hash functions](http://en.wikipedia.org/wiki/Cryptographic_hash_function) should have the property that it’s infeasible to generate an input that has a given hash (pre-image attack). – Gumbo Jun 14 '13 at 05:22
  • @Gumbo: I agree that they use lookup tables but you are missing my point. If you generate an MD5 for a string on your server and i generate one on mine, is there a possibility that they are different(well, I think it is always same)? If not, that means you just have to find only one piece of the puzzle. For eg: like Kolink said, you know that salt is 3 and hash is 10, so it's not difficult to know that the original string is 7. Let me know if my assumption about universal hash is wrong, then my argument becomes wrong by itself. – Fr0zenFyr Jun 14 '13 at 08:04
  • @Fr0zenFyr MD5 is a completely deterministic function. An input X will always result in the hash Y on any system. However, it’s not a trivial function. Actually, [cryptographic hash function](http://en.wikipedia.org/wiki/Cryptographic_hash_function) should to fulfill [certain criteria](http://en.wikipedia.org/wiki/Cryptographic_hash_function#Properties) which render your assumptions invalid. – Gumbo Jun 14 '13 at 15:51
6
/* you  can match the exact string with table value*/

if(md5("string to match") == $res["hashstring"])
 echo "login correct";
dcaswell
  • 3,137
  • 2
  • 26
  • 25
Mahesan Rv
  • 318
  • 4
  • 4
  • but that string wont be the same for everyone, i mean if there are 1000's of user that password string or id string wont be the same it would be a new string for each user – Sharif Aug 18 '14 at 11:57
5

This question is tagged with PHP. But many people are using Laravel framework now. It might help somebody in future. That's why I answering for Laravel. It's more easy to encrypt and decrypt with internal functions.

$string = 'c4ca4238a0b923820dcc';
$encrypted = \Illuminate\Support\Facades\Crypt::encrypt($string);
$decrypted_string = \Illuminate\Support\Facades\Crypt::decrypt($encrypted);

var_dump($string);
var_dump($encrypted);
var_dump($decrypted_string);

Note: Be sure to set a 16, 24, or 32 character random string in the key option of the config/app.php file. Otherwise, encrypted values will not be secure.

But you should not use encrypt and decrypt for authentication. Rather you should use hash make and check.

To store password in database, make hash of password and then save.

$password = Input::get('password_from_user'); 
$hashed = Hash::make($password); // save $hashed value

To verify password, get password stored of account from database

// $user is database object
// $inputs is Input from user
if( \Illuminate\Support\Facades\Hash::check( $inputs['password'], $user['password']) == false) {
  // Password is not matching 
} else {
  // Password is matching 
}
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • Do you have a link to complete documentation for Crypt? Available modes, padding, password key extension, iv, output format? If not there is no way to interoperate or believe that the encryption is secure. [Encrypter](https://laravel.com/api/5.2/Illuminate/Contracts/Encryption/Encrypter.html) is insufficient. – zaph Jan 30 '16 at 15:34
  • dude its not working your encryption is something different...try to decrypt this – nirav jobanputra May 17 '16 at 06:44
  • and decrypted value should be 123 – nirav jobanputra May 17 '16 at 06:45
  • 202cb962ac59075b964b07152d234b70 here is the encrypted of 123 – nirav jobanputra May 17 '16 at 07:11
5

It's not possible to decrypt MD5 hash which created. You need all information to decrypt the MD5 value which was used during encryption.

You can use AES algorithm to encrypt and decrypt

JavaScript AES encryption and decryption (Advanced Encryption Standard)

Mayur Shedage
  • 1,027
  • 2
  • 11
  • 19
  • 1
    Page not found. Maybe this helps: https://javascript.tutorialink.com/javascript-aes-encryption-and-decryption-advanced-encryption-standard/ – statosdotcom May 07 '22 at 13:00
2

Hashes can not be decrypted check this out.

If you want to encrypt-decrypt, use a two way encryption function of your database like - AES_ENCRYPT (in MySQL).

But I'll suggest CRYPT_BLOWFISH algorithm for storing password. Read this- http://php.net/manual/en/function.crypt.php and http://us2.php.net/manual/en/function.password-hash.php

For Blowfish by crypt() function -

crypt('String', '$2a$07$twentytwocharactersalt$');

password_hash will be introduced in PHP 5.5.

$options = [
    'cost' => 7,
    'salt' => 'BCryptRequires22Chrcts',
];
password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);

Once you have stored the password, you can then check if the user has entered correct password by hashing it again and comparing it with the stored value.

ShuklaSannidhya
  • 8,572
  • 9
  • 32
  • 45
  • 3
    The new function `password_hash()` is a good recommendation, but it looses some of its benefit, when you pass your own salt. Just let the function generate the salt, it does it in a cryptographically safe way. – martinstoeckli Mar 04 '13 at 08:46