Forum Moderators: coopster

Message Too Old, No Replies

PHP and SQL encrypting data before sending to database

         

echo

10:57 am on Dec 3, 2003 (gmt 0)

10+ Year Member



I know it is possible but dont know how to do it!

how can you encrypt data using php before sending it to a MySQL database (e.g password) and then dycrypt it when retriving it.

Any Suggestions ( I assume there is a simple function eg: encrypt($password) )

Thanks people

ukgimp

11:04 am on Dec 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld

How weird. I was just about to look into this myself. Will post when I find more but mycrypt is looking good.

[uk.php.net...]

ukgimp

11:06 am on Dec 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[uk2.php.net...]

strtr()

Easier to crack version. But that depends on how secure you wish the stuff to be.

If you dont need to decrypt it then MD5 it
[uk2.php.net...]

ukgimp

11:10 am on Dec 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This could be the one

reversible encryption routine for PHP [tonymarston.net]

echo

11:30 am on Dec 3, 2003 (gmt 0)

10+ Year Member



I think mcrypt is the best option and this is automatically included as part of the latest version of PHP.

for details how to use it try here:
[php.planetmirror.com...]

dcrombie

12:13 pm on Dec 3, 2003 (gmt 0)



Why do you need to decrypt the passwords? The whole point of password encryption is that is should be one-way. To test a password against the stored crypt, you just need to crypt the input value using the stored crypt as the salt.

Does that make any sense?

$username = <user input>;
$password = <user input>;
$pwcrypt = crypt ($password);
doQuery ("INSERT INTO users VALUES ('$username', '$pwcrypt')");

---

$username = <user input>;
$password = <user input>;
doQuery ("SELECT pwcrypt FROM users WHERE username='$username');
if (crypt ($password, $pwcrypt) == $pwcrypt) {
# password matches;
} else {
# does not match;
}

daosmith

1:04 pm on Dec 3, 2003 (gmt 0)

10+ Year Member



A problem with a lot of these methods is that the password must be sent in clear text from the client machine to the PHP server before it can be encrypted. If it's available you should see if you can get a secure connection (https).

I'm testing a site where the user's password is encrypted using a javascript version of md5, so the data is encrypted before it even gets onto the internet, but the downside is that you have to force the user to have javascript turned on in their browser, which apaprently 10% of the browsing population do not. Still, if security is important to your db, it may be worth it.

jatar_k

5:34 pm on Dec 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you want it to be secure

1. over https
2. store passwords as MD5 hash
3. take input, MD5 it, compare with stored hash (as mentioned above)

passwords should never be unencryptable, no reason to. If the user forgets their password they can get a new generated one sent to the email attributed to their account and then on login they should be prompted to change it from the generated one to something they can remember.