-1

Hi am using hashing for my project. I was created encryption correctly that code is following:

<?php 
function cryptPass($input, $rounds = 9) {
    $salt = "";
    $saltChars = array_merge(range('A','Z'), range('a','z'), range('0','1'));
    for($i = 0; $i < 22; $i++)
    {
        $salt .=$saltChars[array_rand($saltChars)];
    }
    return crypt($input, sprintf('$2y$%02d$', $rounds). $salt);
}
$pass = "passsword";
$hasedpass = cryptPass($pass);
echo $hasedpass;
echo '<br>';
?>

My result is: $2y$09$HICRjrIyBYXWqcqRFC1dDOXF9tTtKZeOTBewebsooxHtWvvepqrnu

Now my question is how to decrypt $hasedpass. I mean the result will be come password $pass values.

Ramesh S
  • 37
  • 7
  • 3
    From the [docs](http://php.net/manual/en/function.crypt.php): “ There is no decrypt function, since crypt() uses a one-way algorithm”. – fusion3k Feb 27 '16 at 14:10
  • ya ok you are right ,but if am using login script means how can i declare `$hasedpass` same from database values – Ramesh S Feb 27 '16 at 14:14
  • With your own function, I don't know. Generally, you can use [`hash_equals()`](http://php.net/manual/en/function.hash-equals.php). See also [php passwords FAQ](http://php.net/manual/en/faq.passwords.php) – fusion3k Feb 27 '16 at 14:29
  • You have to store the salt if you want to compare the password + hash to the crypted password later. – A.L Feb 27 '16 at 14:34

1 Answers1

3

From the documentation:

crypt — One-way string hashing

You can't decrypt a one-way hash. That is the point of using a one-way hash.

If you want to find out if a password is correct then take the submitted password, crypt it with the same salt, then compare the result to the stored, hashed version.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335