User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,543 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,305 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1982 | Replies: 3
Reply
Join Date: Jul 2007
Posts: 23
Reputation: ezb is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
ezb ezb is offline Offline
Newbie Poster

Password encoding/decoding

  #1  
Oct 15th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: New Jersey
Posts: 1,422
Reputation: stymiee is on a distinguished road 
Rep Power: 5
Solved Threads: 34
Moderator
stymiee's Avatar
stymiee stymiee is offline Offline
He's No Good To Me Dead

Re: Password encoding/decoding

  #2  
Oct 15th, 2007
Here is a good PHP5 class that uses the mcrypt library for two way encryption.

  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.
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Reply With Quote  
Join Date: Jul 2007
Posts: 23
Reputation: ezb is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
ezb ezb is offline Offline
Newbie Poster

Re: Password encoding/decoding

  #3  
Oct 15th, 2007
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?
Reply With Quote  
Join Date: May 2006
Location: New Jersey
Posts: 1,422
Reputation: stymiee is on a distinguished road 
Rep Power: 5
Solved Threads: 34
Moderator
stymiee's Avatar
stymiee stymiee is offline Offline
He's No Good To Me Dead

Re: Password encoding/decoding

  #4  
Oct 16th, 2007
It can be changed to work with PHP 4. You just need to change the PHP 5 features to 4:

  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. ?>
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 5:00 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC