1

I was using PHP's crypt() function to encrypt the password before storing into the database.Now if the password contains number then all passwords with same sub string generates the same encryption. For example all the below passwords generate the same encryption.

echo crypt('abcdefg123','mykey').'<br>';
echo crypt('abcdefg123','mykey').'<br>';
echo crypt('abcdefg123456','mykey').'<br>';

Encrypted password result is

myeWT99Ku6TaM

What am I doing wrong? or is it a bug?

Akhilesh
  • 1,243
  • 4
  • 16
  • 49
  • Uses only first 8 characters to generate hash. Since first 8 characters in all the your inputs are same, you will get the same result. Also the generated hash will be < = 13 characters – Tushar Gupta Sep 30 '15 at 10:49
  • I would also suggest you to check this for password hashing in php: http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php – Night2 Sep 30 '15 at 10:51
  • `crypt()` does not provide [encryption](https://paragonie.com/blog/2015/08/you-wouldnt-base64-a-password-cryptography-decoded), it provides password hashing. Password hashing is cryptography, but it is not encryption. – Scott Arciszewski Sep 30 '15 at 14:02

2 Answers2

2

crypt function takes salt as the second argument. salt has special formats described here.
You have provided a salt which is recognized as standard DES algorithm.

The standard DES-based crypt() returns the salt as the first two characters of the output. It also only uses the first eight characters of str, so longer strings that start with the same eight characters will generate the same result (when the same salt is used).

Provide the proper salt. For example, try this for MD5:

echo crypt('abcdefg123','$1$mykeyabcd$').'<br>';
echo crypt('abcdefg123','$1$mykeyabcd$').'<br>';
echo crypt('abcdefg123456','$1$mykeyabcd$').'<br>';
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

I think it's because of salt. By default you propably use CRYPT_STD_DES hash type in crypt function and this type works with two character salt, but you use 5 character salt.

Using invalid characters in the salt will cause crypt() to fail. http://php.net/manual/ru/function.crypt.php

karma_police
  • 332
  • 2
  • 14