00001 <?php
00030 class zCryptDrv_mcrypt extends zCore{
00031 var $iv;
00032 var $key;
00033 var $iv_size;
00034 var $div;
00035
00036 function zCryptDrv_mcrypt(){
00037 $this->__construct();
00038 }
00039 function __construct(){
00040 parent::__construct();
00041 if(!defined('ENC_BIN')){define('ENC_BIN', null);}
00042 if(!defined('ENC_DEC')){define('ENC_DEC', null);}
00043 $tmp_deff = parse_ini_file(ZIGROOT.DRS.Z_DIR_PROJECTS.DRS.Z_PROJECT_DIR.DRS.ZIGSETTINGSDIR.DRS.'mcpt.ini.php', false);
00044 foreach($tmp_deff as $key => $val){
00045 if($key != 'iv' && $key != 'key'){
00046 if(!defined($key)){define($key, $val);}
00047 }
00048 }
00049 $this->iv = base64_decode($tmp_deff['iv']);
00050 $this->key = md5($tmp_deff['mkey']);
00051
00052 $this->iv_size = mcrypt_get_iv_size(SEC_MCRYPT_ALGO, SEC_MCRYPT_MODE);
00053 $this->div = mcrypt_create_iv($this->iv_size, SEC_MCRYPT_IV_SRC);
00054 }
00060 function mct(){
00061 list($usec, $sec) = explode(" ", microtime());
00062 return ((float)$usec + (float)$sec);
00063 }
00076 function encode($str, $key = NULL){
00077 if(is_null($key)){$key = $this->key;}
00078 $key_size = mcrypt_get_key_size(SEC_MCRYPT_ALGO, SEC_MCRYPT_MODE);
00079 if(strlen($key) > $key_size){
00080
00081 $key = substr($key, 0, $key_size);
00082 }
00083 return mcrypt_encrypt(SEC_MCRYPT_ALGO, $key, base64_encode($str), SEC_MCRYPT_MODE, $this->iv);
00084 }
00095 function decode($str, $key = NULL){
00096 if(is_null($key)){$key = $this->key;}
00097 $key_size = mcrypt_get_key_size(SEC_MCRYPT_ALGO, SEC_MCRYPT_MODE);
00098 if(strlen($key) > $key_size){
00099
00100 $key = substr($key, 0, $key_size);
00101 }
00102
00103 return base64_decode(trim(@mcrypt_decrypt(SEC_MCRYPT_ALGO, $key, $str, SEC_MCRYPT_MODE, $this->iv)));
00104 }
00121 function denc($str, $key = NULL){
00122 if(is_null($key)){$key = $this->key;}
00123 $key_size = mcrypt_get_key_size(SEC_MCRYPT_ALGO, SEC_MCRYPT_MODE);
00124 if(strlen($key) > $key_size){
00125
00126 $key = substr($key, 0, $key_size);
00127 }
00128 return mcrypt_encrypt(SEC_MCRYPT_ALGO, $key, base64_encode($str), SEC_MCRYPT_MODE, $this->div);
00129 }
00139 function ddcd($str, $key = NULL){
00140 if(is_null($key)){$key = $this->key;}
00141 $key_size = mcrypt_get_key_size(SEC_MCRYPT_ALGO, SEC_MCRYPT_MODE);
00142 if(strlen($key) > $key_size){
00143
00144 $key = substr($key, 0, $key_size);
00145 }
00146 return trim(base64_decode(mcrypt_decrypt(SEC_MCRYPT_ALGO, $key, $str, SEC_MCRYPT_MODE, $this->div)));
00147 }
00161 function tenc($str, $key = NULL){
00162 if(is_null($key)){$key = $this->key;}
00163 $key_size = mcrypt_get_key_size(SEC_MCRYPT_ALGO, SEC_MCRYPT_MODE);
00164 if(strlen($key) > $key_size){
00165
00166 $key = substr($key, 0, $key_size);
00167 }
00168 if(SEC_MCRYPT_TIMEOUT == 'm'){$t = time();}elseif(SEC_MCRYPT_TIMEOUT == 's'){$t = $this->mct();}
00169 return mcrypt_encrypt(SEC_MCRYPT_ALGO, $key.$t, base64_encode($str), SEC_MCRYPT_MODE, $this->div);
00170 }
00179 function tdcd($str, $key = NULL){
00180 if(is_null($key)){$key = $this->key;}
00181 $key_size = mcrypt_get_key_size(SEC_MCRYPT_ALGO, SEC_MCRYPT_MODE);
00182 if(strlen($key) > $key_size){
00183
00184 $key = substr($key, 0, $key_size);
00185 }
00186 if(SEC_MCRYPT_TIMEOUT == 'm'){$t = time();}elseif(SEC_MCRYPT_TIMEOUT == 's'){$t = $this->mct();}
00187
00188 return base64_decode(trim(mcrypt_decrypt(SEC_MCRYPT_ALGO, $key.$t, $str, SEC_MCRYPT_MODE, $this->div)));
00189 }
00198 function encrypt($str, $key = NULL){
00199 return $this->encode($str, $key);
00200 }
00209 function decrypt($str, $key = NULL){
00210 return $this->decode($str, $key);
00211 }
00212 }
00214 ?>