00001 <?php
00030 class zCryptDrv_xor_intermediate extends zCore{
00031 var $val;
00032
00033 function zCryptDrv_xor_intermediate(){
00034 $this->__construct();
00035 }
00036 function __construct(){
00037 parent::__construct();
00038 if(!defined('BLOCK_SIZE')){define('BLOCK_SIZE', 4);}
00039 if(!defined('BLOCK_JUNK')){define('BLOCK_JUNK', 5);}
00040 if(!defined('ENC_BIN')){define('ENC_BIN', true);}
00041 if(!defined('ENC_DEC')){define('ENC_DEC', false);}
00042 $this->val = array(' ', '+', '_', '*', '&', ':', '-', '@', '=', '%');
00043 }
00051 function blockise($str){
00052 if(strlen($str) >= BLOCK_SIZE*2){return $str;}
00053 while(strlen($str) < BLOCK_SIZE*2){
00054 $str .= chr(BLOCK_JUNK);
00055 }
00056 return $str;
00057 }
00065 function dblockise($str){
00066 for($i=strlen($str)-1;$i>=strlen($str)-1-8;$i--){
00067 if(isset($str[$i]) && $str[$i] != chr(BLOCK_JUNK)){break;}
00068 $str = substr($str, 0, $i);
00069 }
00070 return $str;
00071 }
00080 function psubstr($str, $start, $len){
00081 if($start < 0){$tmp = "";return $tmp;};
00082 return substr($str, $start, $len);
00083 }
00091 function get_block($str, $i){return $this->psubstr($str, $i, BLOCK_SIZE);}
00099 function x_enc($string, $key){
00100 for($i=0; $i<=strlen($string)-1; $i++){
00101 for($j=0; $j<=strlen($key)-1; $j++){
00102 $string[$i] = $string[$i]^$key[$j];
00103 }
00104 if($i != 0 && $i != strlen($string)-1){
00105 $string[$i] = $string[$i]^$string[$i-1]^$string[$i+1];
00106 }
00107 }
00108 return $string;
00109 }
00117 function x_dcd($string, $key){
00118 for($i=strlen($string)-1; $i>=0; $i--){
00119 if($i == 0 || $i == strlen($string)-1){
00120 for($j=0; $j<=strlen($key)-1; $j++){
00121 $string[$i] = $string[$i]^$key[$j];
00122 }
00123 }else{
00124 $string[$i] = $this->x_enc($string[$i], $key)^$string[$i-1]^$string[$i+1];
00125 }
00126 }
00127 return $string;
00128 }
00136 function enc($str, $key){
00137 $str = base64_encode($this->blockise($str));
00138 for($i=0;$i<=strlen($str)-1;$i+=BLOCK_SIZE){
00139 if($i == 0 || $i == (strlen($str)-BLOCK_SIZE)){
00140 $repl = $this->x_enc($this->psubstr($str, $i, BLOCK_SIZE), $key);
00141 $str = substr_replace($str, $repl, $i, BLOCK_SIZE);
00142 }else{
00143 if($i == BLOCK_SIZE){
00144 $b = $this->x_enc($this->get_block($str, $i), $key);
00145 }
00146 $repl = $this->x_enc($this->get_block($str, $i+BLOCK_SIZE), $this->get_block($str, $i-BLOCK_SIZE));
00147 $str = substr_replace($str, $repl, $i, BLOCK_SIZE);
00148 }
00149 }
00150 $str .= $b;
00151 return $str;
00152 }
00160 function dcd($str, $key){
00161 $b = $this->x_dcd($this->get_block($str, strlen($str)-BLOCK_SIZE), $key);
00162 $str = substr($str, 0, strlen($str)-BLOCK_SIZE);
00163 for($i=strlen($str)-BLOCK_SIZE;$i>=0;$i-=BLOCK_SIZE){
00164 if($i == 0 || $i == (strlen($str)-BLOCK_SIZE)){
00165 $repl = $this->x_dcd($this->psubstr($str, $i, BLOCK_SIZE), $key);
00166 $str = substr_replace($str, $repl, $i, BLOCK_SIZE);
00167 }else{
00168 if($i == BLOCK_SIZE){
00169 $str = substr_replace($str, $b, $i, BLOCK_SIZE);
00170 }else{
00171 $repl = $this->x_dcd($this->psubstr($str, $i-BLOCK_SIZE, BLOCK_SIZE), $this->psubstr($str, $i-(BLOCK_SIZE*2), BLOCK_SIZE));
00172 $str = substr_replace($str, $repl, $i, BLOCK_SIZE);
00173 }
00174 }
00175 }
00176 return $this->dblockise(base64_decode($str));
00177 }
00187 function encode($str, $key, $mode = ENC_BIN){
00188 if($mode == ENC_BIN){
00189 return $this->enc($this->enc($str, $key), md5($key));
00190 }elseif($mode == ENC_DEC){
00191 return $this->dec_encode($this->enc($this->enc($str, $key), md5($key)));
00192 }
00193 perror("Invalid MODE SPecified");
00194 return false;
00195 }
00205 function decode($str, $key, $mode = ENC_BIN){
00206 if($mode == ENC_BIN){
00207 return $this->dcd($this->dcd($str, md5($key)), $key);
00208 }elseif($mode == ENC_DEC){
00209 return $this->dcd($this->dcd($this->dec_decode($str), md5($key)),$key);
00210 }
00211 perror("Invalid MODE SPecified");
00212 return false;
00213 }
00220 function dec_decode($salted){
00221 $key = array(0,1,2,3,4,5,6,7,8,9);$ret = '';
00222 $val = $this->val;
00223 $str = str_replace($val, $key, $salted);
00224 for($i=0;$i<=strlen($str)-1;$i+=3){
00225 $current_group = $str[$i].$str[$i+1].$str[$i+2];
00226 $tmp_int = (int)($current_group);
00227 $ret .= chr($tmp_int);
00228 }
00229 return base64_decode($ret);
00230 }
00237 function dec_encode($salt){
00238 $str = base64_encode($salt);$tmp = '';
00239 for($i=0;$i<=strlen($str)-1;$i++){
00240 if(ord($str[$i]) < 100){
00241 if(ord($str[$i]) > 0 && ord($str[$i]) < 10){
00242 $num = "00".ord($str[$i]);
00243 }
00244 elseif(ord($str[$i]) >= 10 && ord($str[$i]) < 100){
00245 $num = "0".ord($str[$i]);
00246 }
00247 }
00248 else{
00249 $num = ord($str[$i]);
00250 }
00251 $tmp .= $num;
00252 }
00253 $key = array(0,1,2,3,4,5,6,7,8,9);
00254 $val = $this->val;
00255 $ret = str_replace($key, $val, $tmp);
00256 return $ret;
00257 }
00258 }
00263 class zCryptDrv_xor extends zCryptDrv_xor_intermediate {
00264 var $key;
00265
00266 function zCryptDrv_xor(){
00267 $this->__construct();
00268 }
00269 function __construct(){
00270 parent::__construct();
00271 $tmp = parse_ini_file(ZIGROOT.DRS.Z_DIR_PROJECTS.DRS.Z_PROJECT_DIR.DRS.ZIGSETTINGSDIR.DRS.'zcpt.ini.php', false);
00272 foreach($tmp as $key => $val){
00273 if($key != 'key'){
00274 if($key == "ZCRYPT_DEFAULT_MODE"){
00275 if($val == "dec"){
00276 $val = ENC_DEC;
00277 }elseif($val == "bin"){
00278 $val = ENC_BIN;
00279 }else{
00280 perror("Invalid ZCRYPT_DEFAULT_MODE ".$val." on ".'zcpt.ini.php'." Must be dec or bin");
00281 }
00282 }
00283 if(!defined($key)){define($key, $val);}
00284 }
00285 }
00286 $this->key = $tmp['key'];
00287 }
00301 function encrypt(){
00302 $list_args = func_get_args();
00303 if(@$list_args[1] == null){unset($list_args[1]);}
00304 if(count($list_args) == 0){
00305 perror("You must supply the string to encrypt as first argument 0 Arguments Given");
00306 exit(1);
00307 }
00308 if(!is_string($list_args[0])){
00309 perror("You must supply the string to encrypt as first argument<br />and that should be String ".gettype($list_args[0])." Given");
00310 exit(1);
00311 }
00312 if(count($list_args) > 3){
00313 perror("encrypt() requires atleast 1 and atmost 3 arguments ".count($list_args)." given");
00314 }
00315 if(count($list_args) == 3){
00316 return $this->encode($list_args[0], $list_args[1], $list_args[2]);
00317 }
00318 if(count($list_args) == 2){
00319 if(is_string($list_args[1])){
00320 $key = $list_args[1];
00321 $mode = ZCRYPT_DEFAULT_MODE;
00322 }elseif(is_bool($list_args[1])){
00323 $key = $this->key;
00324 $mode = $list_args[1];
00325 }else{
00326 perror("2 arguments Given to encrypt() in which second argument is nither MODE nor key rather its ".gettype($list_args[1])." Whos's value is ".$list_args[1]);
00327 }
00328 return $this->encode($list_args[0], $key, $mode);
00329 }else{
00330 return $this->encode($list_args[0], $this->key, ZCRYPT_DEFAULT_MODE);
00331 }
00332 }
00346 function decrypt(){
00347 $list_args = func_get_args();
00348 if(@$list_args[1] == null){unset($list_args[1]);}
00349 if(count($list_args) == 0){
00350 perror("You must supply the string to encrypt as first argument 0 Arguments Given");
00351 exit(1);
00352 }
00353 if(!is_string($list_args[0])){
00354 perror("You must supply the string to encrypt as first argument<br />and that should be String ".gettype($list_args[0])." Given");
00355 exit(1);
00356 }
00357 if(count($list_args) > 3){
00358 perror("encrypt() requires atleast 1 and atmost 3 arguments ".count($list_args)." given");
00359 }
00360 if(count($list_args) == 3){
00361 return $this->decode($list_args[0], $list_args[1], $list_args[2]);
00362 }
00363 if(count($list_args) == 2){
00364 if(is_string($list_args[1])){
00365 $key = $list_args[1];
00366 $mode = ZCRYPT_DEFAULT_MODE;
00367 }elseif(is_bool($list_args[1])){
00368 $key = $this->key;
00369 $mode = $list_args[1];
00370 }else{
00371 perror("2 arguments Given to encrypt() in which second argument is nither MODE nor key");
00372 }
00373 return $this->decode($list_args[0], $key, $mode);
00374 }else{
00375 return $this->decode($list_args[0], $this->key, ZCRYPT_DEFAULT_MODE);
00376 }
00377 }
00378 }
00380 ?>