944,008 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 12334
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 15th, 2007
0

Password encoding/decoding

Expand Post »
I am currently building an online system, it has come to the point to think about securing peoples passwords. How ever, for admin reasons I was wondering if it was possible to decode the encoded password, I believe this is not possible with md5 but hoping there is another method?

Any help would be geat, also any other information regarding safety, thanks.
Similar Threads
ezb
Reputation Points: 10
Solved Threads: 3
Newbie Poster
ezb is offline Offline
23 posts
since Jul 2007
Oct 15th, 2007
0

Re: Password encoding/decoding

Here is a good PHP5 class that uses the mcrypt library for two way encryption.

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. class Encryption
  4. {
  5. static $cypher = 'blowfish';
  6. static $mode = 'cfb';
  7. static $key = '1a2s3d4f5g6h';
  8.  
  9. public function encrypt($plaintext)
  10. {
  11. $td = mcrypt_module_open(self::$cypher, '', self::$mode, '');
  12. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  13. mcrypt_generic_init($td, self::$key, $iv);
  14. $crypttext = mcrypt_generic($td, $plaintext);
  15. mcrypt_generic_deinit($td);
  16. return $iv.$crypttext;
  17. }
  18.  
  19. public function decrypt($crypttext)
  20. {
  21. $plaintext = "";
  22. $td = mcrypt_module_open(self::$cypher, '', self::$mode, '');
  23. $ivsize = mcrypt_enc_get_iv_size($td);
  24. $iv = substr($crypttext, 0, $ivsize);
  25. $crypttext = substr($crypttext, $ivsize);
  26. if ($iv)
  27. {
  28. mcrypt_generic_init($td, self::$key, $iv);
  29. $plaintext = mdecrypt_generic($td, $crypttext);
  30. }
  31. return $plaintext;
  32. }
  33. }
  34.  
  35. // Encrypt text
  36. $encrypted_text = Encryption::encrypt('this text is unencrypted');
  37.  
  38. // Decrypt text
  39. $decrypted_text = Encryption::decrypt($encrypted_text);
  40.  
  41.  
  42. ?>
Last edited by stymiee; Oct 15th, 2007 at 11:48 am.
Moderator
Reputation Points: 161
Solved Threads: 38
He's No Good To Me Dead
stymiee is offline Offline
1,422 posts
since May 2006
Oct 15th, 2007
0

Re: Password encoding/decoding

Thanks alot for your help, however, I am using 4.3.9, sorry I should have mentioned this to begin with, the code you gave strictly php5?
ezb
Reputation Points: 10
Solved Threads: 3
Newbie Poster
ezb is offline Offline
23 posts
since Jul 2007
Oct 16th, 2007
0

Re: Password encoding/decoding

It can be changed to work with PHP 4. You just need to change the PHP 5 features to 4:

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. class Encryption
  4. {
  5. var $cypher = 'blowfish';
  6. var $mode = 'cfb';
  7. var $key = '1a2s3d4f5g6h';
  8.  
  9. function Encryption()
  10. {
  11. // do nothing
  12. }
  13.  
  14. function encrypt($plaintext)
  15. {
  16. $td = mcrypt_module_open($this->cypher, '', $this->mode, '');
  17. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  18. mcrypt_generic_init($td, $this->key, $iv);
  19. $crypttext = mcrypt_generic($td, $plaintext);
  20. mcrypt_generic_deinit($td);
  21. return $iv.$crypttext;
  22. }
  23.  
  24. function decrypt($crypttext)
  25. {
  26. $plaintext = "";
  27.  
  28. $td = mcrypt_module_open($this->cypher, '', $this->mode, '');
  29. $ivsize = mcrypt_enc_get_iv_size($td);
  30. $iv = substr($crypttext, 0, $ivsize);
  31. $crypttext = substr($crypttext, $ivsize);
  32. if ($iv)
  33. {
  34. mcrypt_generic_init($td, $this->key, $iv);
  35. $plaintext = mdecrypt_generic($td, $crypttext);
  36. }
  37. return $plaintext;
  38. }
  39. }
  40.  
  41. ?>
Moderator
Reputation Points: 161
Solved Threads: 38
He's No Good To Me Dead
stymiee is offline Offline
1,422 posts
since May 2006
Nov 5th, 2009
-1
Re: Password encoding/decoding
can this store, say for instance, a PayPal token that I am supposed to keep hidden?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bennyfreshness is offline Offline
1 posts
since Nov 2009
Nov 5th, 2009
0
Re: Password encoding/decoding
You can do it at the database level as well if you want.

For INSERT
PHP Syntax (Toggle Plain Text)
  1. $aes_key = "EF77FHH7-E6G1-31y4-w2D7-G4gH8HWF20H1";
  2. $sql = "INSERT INTO user(username, pass) VALUES ('bob', AES_ENCRYPT('password', '$aes_key' ))";

And for SELECT
PHP Syntax (Toggle Plain Text)
  1. $aes_key = "EF77FHH7-E6G1-31y4-w2D7-G4gH8HWF20H1";
  2. $sql = "SELECT *, AES_DECRYPT(password, '$aes_key ') AS password FROM user";

You'll have to keep the AES key as a config value or something. If you lose it you can't decrypt the data

More references here.
Last edited by jomanlk; Nov 5th, 2009 at 4:05 am. Reason: Corrected code error
Reputation Points: 13
Solved Threads: 19
Junior Poster
jomanlk is offline Offline
103 posts
since Oct 2009
Nov 5th, 2009
0
Re: Password encoding/decoding
Hey.

Be careful if you do this in a SQL query tho. Some MySQL servers use plain-text query logs, so while your passwords might be encrypted in the database itself, they would be stored in their original form in the logs.

See these two pages in the manual for details on that.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 5th, 2009
0
Re: Password encoding/decoding
@Atli
Good point. I didn't know this. This can be a problem if your MySQL server is not controlled by you alone.
Reputation Points: 13
Solved Threads: 19
Junior Poster
jomanlk is offline Offline
103 posts
since Oct 2009
Nov 5th, 2009
1
Re: Password encoding/decoding
Click to Expand / Collapse  Quote originally posted by ezb ...
I am currently building an online system, it has come to the point to think about securing peoples passwords. How ever, for admin reasons I was wondering if it was possible to decode the encoded password, I believe this is not possible with md5 but hoping there is another method?

Any help would be geat, also any other information regarding safety, thanks.
There really is no reason to use 2 way encryption on passwords. Retrieving the password is not the concern, gaining access to their account is. So if the user forgets their password, send them a token through email to set a new password.

Use secure hashes to store the passwords. Add a long salt before hashing, and hash that password and salt together 100,000 times or so. Make sure you use quite a bit of memory in the process.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 5th, 2009
0
Re: Password encoding/decoding
@digital-ether
I agree with you 100%, although 100.000 iterations seem a bit excessive to me. (But that's just me :-P)
However, I got to ask why you specifically mention high memory usage?
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: To restrict users to choose date from date picker only
Next Thread in PHP Forum Timeline: Messaging System





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC