00001 <?php
00039 class zCrypt extends zCore{
00053 function encrypt($str, $base64 = false){
00054 global ${Z_IN_CRYPT_VAR_NAME};
00055 $crypt = &${Z_IN_CRYPT_VAR_NAME};
00056 if(!is_array($str)){
00057 if($base64){
00058 return base64_encode($crypt->encrypt((string)$str));
00059 }
00060 return $crypt->encrypt((string)$str);
00061 }else{
00062 if(array_walk_recursive($str, array('zCrypt', 'encrypt'), array_pad(array(), count($str), $base64)))
00063 return $str;
00064 else
00065 return false;
00066 }
00067 }
00077 function decrypt($str, $base64 = false){
00078 global ${Z_IN_CRYPT_VAR_NAME};
00079 $crypt = &${Z_IN_CRYPT_VAR_NAME};
00080 if(!is_array($str)){
00081 if($base64){
00082 return $crypt->decrypt(base64_decode($str));
00083 }
00084 return $crypt->decrypt((string)$str);
00085 }else{
00086 foreach($str as $key => $val){
00087 $str[$key] = zCrypt::decrypt($val, $base64);
00088 }
00089 return $str;
00090 }
00091 }
00099 function encode($str, $base64 = false){
00100 return zCrypt::encrypt($str, $base64);
00101 }
00109 function decode($str, $base64 = false){
00110 return zCrypt::decrypt($str, $base64);
00111 }
00119 function enc($str, $base64 = false){
00120 return zCrypt::encrypt($str, $base64);
00121 }
00129 function dcd($str, $base64 = false){
00130 return zCrypt::decrypt($str, $base64);
00131 }
00141 function hash($str){
00142 switch(Z_HASH_ALGO){
00143 case 'Z__php_str_md5':
00144 $ret = md5($str);
00145 break;
00146 case 'Z__php_str_sha1':
00147 $ret = sha1($str);
00148 break;
00149 default:
00150 $ret = bin2hex(mhash(Z_HASH_ALGO, $str));
00151 break;
00152 }
00153 if(!Z_HASH_ENC){
00154 return $ret;
00155 }else{
00156 return zCrypt::encrypt($ret, true);
00157 }
00158 }
00169 function denc($str, $key = null){
00170 global ${Z_IN_CRYPT_VAR_NAME};
00171 $crypt = &${Z_IN_CRYPT_VAR_NAME};
00172 if(!method_exists($crypt, 'denc')){
00173 perror('Sorry you must use either mcrypt or both xor and mcrypt as Encryption driver to use this function CURRENTLY');
00174 return false;
00175 }
00176 return $crypt->denc((string)$str, $key);
00177 }
00186 function ddcd($str, $key = null){
00187 global ${Z_IN_CRYPT_VAR_NAME};
00188 $crypt = &${Z_IN_CRYPT_VAR_NAME};
00189 if(!method_exists($crypt, 'ddcd')){
00190 perror('Sorry you must use either mcrypt or both xor and mcrypt as Encryption driver to use this function CURRENTLY');
00191 return false;
00192 }
00193 return $crypt->ddcd($str, $key);
00194 }
00200 function mcrypt_aviliable(){
00201 if(function_exists('mcrypt_module_open')){
00202 return true;
00203 }
00204 else return false;
00205 }
00213 function menc($string,$key = null){
00214 global $app_data;
00215 if(!mcrypt_aviliable()){
00216 perror('Sorry Mcrypt is not aviliable');
00217 return false;
00218 }
00219 if($key == null){
00220 $data = parse_ini_file(ZIGROOT.DRS.Z_DIR_PROJECTS.DRS.Z_PROJECT_DIR.DRS.ZIGSETTINGSDIR.DRS.'mcpt.ini.php', false);
00221 $key = $data['mkey'];
00222 }
00223 srand((double) microtime() * 1000000);
00224 $key = md5($key);
00225 $key_size = mcrypt_get_key_size(SEC_MCRYPT_ALGO, SEC_MCRYPT_MODE);
00226 if(strlen($key) > $key_size){
00227
00228 $key = substr($key, 0, $key_size);
00229 }
00230 $td = mcrypt_module_open(SEC_MCRYPT_ALGO, '',SEC_MCRYPT_MODE, '');
00231 $key = substr($key, 0, mcrypt_enc_get_key_size($td));
00232 $iv_size = mcrypt_enc_get_iv_size($td);
00233 $iv = mcrypt_create_iv($iv_size, SEC_MCRYPT_IV_SRC);
00234 if(mcrypt_generic_init($td, $key, $iv) != -1){
00235 $c_t = mcrypt_generic($td, $string);
00236 mcrypt_generic_deinit($td);
00237 mcrypt_module_close($td);
00238 $c_t = $iv.$c_t;
00239 return $c_t;
00240 }
00241 }
00249 function mdcd($string,$key = null){
00250 if(!mcrypt_aviliable()){
00251 perror('Sorry Mcrypt is not aviliable');
00252 return false;
00253 }
00254 if($key == null){
00255 $data = parse_ini_file(ZIGROOT.DRS.Z_DIR_PROJECTS.DRS.Z_PROJECT_DIR.DRS.ZIGSETTINGSDIR.DRS.'mcpt.ini.php', false);
00256 $key = $data['mkey'];
00257 }
00258 $key = md5($key);
00259 $key_size = mcrypt_get_key_size(SEC_MCRYPT_ALGO, SEC_MCRYPT_MODE);
00260 if(strlen($key) > $key_size){
00261
00262 $key = substr($key, 0, $key_size);
00263 }
00264 $td = mcrypt_module_open(SEC_MCRYPT_ALGO, '',SEC_MCRYPT_MODE, '');
00265 $key = substr($key, 0, mcrypt_enc_get_key_size($td));
00266 $iv_size = mcrypt_enc_get_iv_size($td);
00267 $iv = substr($string,0,$iv_size);
00268 $string = substr($string,$iv_size);
00269 if (mcrypt_generic_init($td, $key, $iv) != -1){
00270 $c_t = mdecrypt_generic($td, $string);
00271 mcrypt_generic_deinit($td);
00272 mcrypt_module_close($td);
00273 return $c_t;
00274 }
00275 }
00286 function tenc($str, $key = null){
00287 global ${Z_IN_CRYPT_VAR_NAME};
00288 $crypt = &${Z_IN_CRYPT_VAR_NAME};
00289 if(!method_exists($crypt, 'tenc')){
00290 perror('Sorry you must use either mcrypt or both xor and mcrypt as Encryption driver to use this function CURRENTLY');
00291 return false;
00292 }
00293 return $crypt->tenc($str, $key);
00294 }
00302 function tdcd($str, $key = null){
00303 global ${Z_IN_CRYPT_VAR_NAME};
00304 $crypt = &${Z_IN_CRYPT_VAR_NAME};
00305 if(!method_exists($crypt, 'tenc')){
00306 perror('Sorry you must use either mcrypt or both xor and mcrypt as Encryption driver to use this function CURRENTLY');
00307 return false;
00308 }
00309 return $crypt->tdcd($str, $key);
00310 }
00311 }
00313 ?>