Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

crypt gives the same result

Status
Not open for further replies.

breaststroke

Programmer
Apr 10, 2011
39
ES
Hello,

it is the first time I use crypt function.
I just wanted to secure some data but nothing complicated.

I use it this way:
I retrieve a variable, for instance: $variable.
Then I create a salt everytime I want to encrypt some data.
So, I do this:

$newvalue=crypt($variable, $salt);
Then I keep $newvalue and $salt into the database.

I realised there is a problem when trying to decrypt those data (passwords, in this case).
For instance, if I want to check if a password given by an user is already in the database.
What I do is checking if the salt values in the database, when used along with the new password, match any $newvalue in the database.
I have found out that, for instance, if I firstly used the $variable: pepepepe, it will match (its $newvalue) the value of the $newvalue got by using $variable (the new password): pepepepeno.
It matches also other variations.
So, I mean, this method (crypt) doesn´t seem to be reliable to me, it doesn´t seem to create unique values (as I have seen, different $variable values can generate the same $newvalue values).


To summarize (sorry if I didn´t explain myself properly):
For instance:
$variable1="pepepepe";
$variable2="pepepepeno";
$salt="qqwwee111111111";
$newvalue1=($variable1,$salt);
$newvalue2=($variable2,$salt);
It turns out that $newvalue1==$newvalue2;


Could someone, please, tell me what is wrong with crypt (it is supposed to be a method for securing data) and how I could fix it?

Thank you!

enjoy practicing languages at:
 
From the manual

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).

that therefore explains the reason why different strings provide the same hash. they are the same as to the first eight characters.

instead use the built-in wrapper for crypt

Code:
$hash = password_hash($password, PASSWORD_BCRYPT, array('salt'=>$salt));

 
Thank you so much jpadie!,

I swear I checked the manual but didn´t see that (sure I didn´t check enough).

I´ll try what you suggested

Thanks a million

enjoy practicing languages at:
 
btw - I am not convinced that using crypt is a great solution for password hashing. unless you are working in a very secure industry, I suspect that using sha1 or even md5 is better. you can 'salt' both of those by appending data to the beginning or end of the unhashed string and best of all mysql can handle both ciphers natively.

if you want to hash using an algorithm like blowfish, then one way of reducing the need to store the salt is to use a crypt of the password as the salt itself.

Code:
password_hash($password, PASSWORD_BCRYPT, array('salt'=>password_hash($password, PASSWORD_BCRYPT)));

then you reduce the need for comparing all salts in the database against all potential matches.

and you can, of course, complexify the salt too

Code:
$salt = md5('some random data stored outside the doc root for security');
password_hash($password, PASSWORD_BCRYPT, array('salt'=>password_hash($salt . $password, PASSWORD_BCRYPT)));

but as said, before you go adding loads of complexity and computation cycles, first analyse what level of protection your application really needs. Is there any point in more complex password algorithms than (say) sha1 if, for example, you are not running over https (thus the password is sent in clear text anyway)? or you are using non-hardened servers etc etc. ie. there is no point in having a level 10 lock on your front-door if you have only a level 3 on the your back-door, or windows that you can open with a credit card etc.


 
Thanks again jpadie,

it seems like bcrypt is not supported by my PHP version (5.2),
I´ll need to find something else.
Or maybe I can add it as library?

I don´t need anything very complex actually, but for instance I read bad opinions on md5.
Regards

enjoy practicing languages at:
 
if you don't like md5 then use sha256 (a 256 bit key length based on sha2). and add a variable.

eg. if your password policy is minimum six characters do this

Code:
$salt = substr($password, -1) . $password{4} . $password{3} . $password{0} . $password{1} . $password{2}; 
$hash = hash('sha256', hash('sha256', $salt) . $password);

unless someone knows the salting algorithm that you are using they cannot (sensibly) use brute force, nor the publicly available lookup tables, to reverse engineer the password from the hash.

but if two people use the same password, then the same hash will appear. if you do not want this then also add the email address or some other unique datum into the salt.

the above (whatever the salt) will always return a 256 bit hash which, when expressed in hex, will be 64 characters. so you will need a database column set at varchar(64) (or even binary(64)).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top