PDA

View Full Version : PHP Encrypt/Decrypt functions


izziy
03-23-2000, 03:30 PM
How do I encrypt some texts in PHP.
For example, password field will be encrypted and stored
in the database, and later to check for comparision purpose.

I tried to use crypt(), throw in the same input string, salt key, but it generated two completely different encrypted text.

Any suggestions??

Thanks!

Justin
03-23-2000, 05:20 PM
If you want a simple one-way encryption, try this:
</font><font face="Courier" size="3">
$string = md5 ($string);
</font><font face="Verdana, Arial" size="2">
It's simple and it's one way, and you will always get back the same encrypted string...

Hope this helps.

------------------
Justin Nelson
FutureQuest (http://www.FutureQuest.net/index.php) Support

izziy
03-24-2000, 12:43 PM
Justin:

Thanks for your help...
I used md5($mypass), and gave the same input strings, but I
got two different outcome for one way encryption.
password = d41d8cd98f00b204e9800998ecf8427e
password2 = ad0234829205b9033196ba818f7a87

So, I stored the first password in database, and then
when user enter the password again, I tried to encrypt it
and make comparision with the first one, didn't match.

Also, do you know any sample code or third party apps
that allow me do 2 ways encrypt/decrypt in PHP??

Thanks again

urban
03-24-2000, 03:16 PM
If the database you are using is MySQL, you can use the built-in ENCODE/DECODE functions.


insert into mytable (passwd) values(ENCODE('mypassword',$salt));



select DECODE(passwd,$salt) from mytable where...


Of course, this is irrelevant if you're not using MySQL...

Hunkorama417
03-25-2000, 12:45 AM
Urban, I'm using the code you syggested and the passwords are completely different. Yesterday, things were working fine and all, but today the encrypted passwords don't match the encrypted passwords in the mySQL database. Looks like I'm going to have to scrape the encryption method.
------------------
-Don't worry, perfection isn't for everyone.

Justin
03-25-2000, 01:25 AM
In my tests (and real-world use), the md5() function always returns the same string for the same input string - I use this quite regularly and I know it works this way... Are you sure you are not re-encrypting the password a second time?

At any rate, I know there are some two-way encryptions in the mcrypt() library, but I would hold off on that, as my first attempts with PHP 3.0.15 have been unsuccessful so far... I'm not sure if there's a problem in the mcrypt() library or something else, but I do know that there are some obscure bugs in the .15 version of PHP...

------------------
Justin Nelson
FutureQuest (http://www.FutureQuest.net/index.php) Support

urban
03-26-2000, 10:32 PM
That shouldn't be the case.[nbsp][nbsp]If you're using the exact same value for the $salt variable for the ENCODE and the DECODE, you should always have a matching string.[nbsp][nbsp]

Perhaps if you post your table structure and a relevant code sample, we could figure it out.

What is the column type of the password column?[nbsp][nbsp]How long are the passwords and salt values...

Make sure your column definition is long enough so that the ENCODEd version of the password is not being truncated.

Hunkorama417
03-26-2000, 10:52 PM
I figured it out. Sorry!
------------------
Bill Gates Bill Gates
A man full of greed and hate.

He likes to take your money
When he writes new software for his company.