PHP版DES加密解密

作者 : admin 于 2008年11月12日, 13:31:27
2008
11-12
  1. < ?php
  2. class DES
  3. {
  4. public $key = '';
  5. public $iv = '';
  6. public $td = '';
  7. public $ks = '';
  8. //构造函数
  9. public function __construct( $key )
  10. {
  11. $this->td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
  12. $this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), (substr(PHP_OS,0,3)=='WIN' ? MCRYPT_RAND : MCRYPT_DEV_RANDOM));
  13. $this->ks = mcrypt_enc_get_key_size($this->td);
  14. $this->key = substr(md5($key), 0, $this->ks);
  15. }
  16. //加密函数
  17. function encrypt( $value )
  18.     {
  19. mcrypt_generic_init($this->td, $this->key, $this->iv);
  20. $r = mcrypt_generic( $this->td , $value );
  21. mcrypt_generic_deinit($this->td);
  22. return $r;
  23.     }
  24. //解密函数
  25. function decrypt( $value )
  26.     {
  27. mcrypt_generic_init($this->td, $this->key, $this->iv);
  28. $r = mdecrypt_generic($this->td, $value);
  29. mcrypt_generic_deinit($this->td);
  30. return $r;
  31.     }
  32. //西沟函数
  33. function __destruct()
  34. {
  35. mcrypt_module_close($this->td);
  36. }
  37. }
  38.  
  39.  
  40. $des = new DES('FDASFDAS');
  41. $content = file_get_contents('example.php');
  42. echo $content;
  43. echo "<br /><br /><br /><br /><br />";
  44. $encode = $des->encrypt( $content );
  45. echo $encode;
  46. echo "<br /><br /><br /><br /><br />";
  47. $decode = $des->decrypt($encode);
  48. echo $decode;
  49. ?>