PHP版DES加密解密
作者 : admin 于 2008年11月12日, 13:31:27
2008
11-12
11-12
- < ?php
- class DES
- {
- public $key = '';
- public $iv = '';
- public $td = '';
- public $ks = '';
- //构造函数
- public function __construct( $key )
- {
- $this->td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
- $this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), (substr(PHP_OS,0,3)=='WIN' ? MCRYPT_RAND : MCRYPT_DEV_RANDOM));
- $this->ks = mcrypt_enc_get_key_size($this->td);
- $this->key = substr(md5($key), 0, $this->ks);
- }
- //加密函数
- function encrypt( $value )
- {
- mcrypt_generic_init($this->td, $this->key, $this->iv);
- $r = mcrypt_generic( $this->td , $value );
- mcrypt_generic_deinit($this->td);
- return $r;
- }
- //解密函数
- function decrypt( $value )
- {
- mcrypt_generic_init($this->td, $this->key, $this->iv);
- $r = mdecrypt_generic($this->td, $value);
- mcrypt_generic_deinit($this->td);
- return $r;
- }
- //西沟函数
- function __destruct()
- {
- mcrypt_module_close($this->td);
- }
- }
- $des = new DES('FDASFDAS');
- $content = file_get_contents('example.php');
- echo $content;
- echo "<br /><br /><br /><br /><br />";
- $encode = $des->encrypt( $content );
- echo $encode;
- echo "<br /><br /><br /><br /><br />";
- $decode = $des->decrypt($encode);
- echo $decode;
- ?>