(based on XOR Encryption)
Plese post if you find any bugs.
Code: Select all
class crytpo{
/*
Encryptor Class without mcrypt
Author Neel basu
This is a Pre release beta of Zogmoyd's next version's Level 1 Encryption Class
Made on july 15 2007. Its Under GPL Licence
Please see GNU GPL before using it.
you can Use it Untill the above message is intact.
*/
private $val;
public function __construct(){
if(!defined('BLOCK_SIZE')){define('BLOCK_SIZE', 4);}
if(!defined('BLOCK_JUNK')){define('BLOCK_JUNK', 5);}
if(!defined('ENC_BIN')){define('ENC_BIN', true);}
if(!defined('ENC_DEC')){define('ENC_DEC', false);}
$this->val = array(' ', '+', '_', '*', '&', ':', '-', '@', '=', '%');
}
private function blockise($str){
if(strlen($str) >= BLOCK_SIZE*2){return $str;}
while(strlen($str) < BLOCK_SIZE*2){
$str .= chr(BLOCK_JUNK);
}
return $str;
}
private function dblockise($str){
for($i=strlen($str)-1;$i>=strlen($str)-1-8;$i--){
if($str[$i] != chr(BLOCK_JUNK)){break;}
$str = substr($str, 0, $i);
}
return $str;
}
private function psubstr($str, $start, $len){
if($start < 0){$tmp = "";return $tmp;};
return substr($str, $start, $len);
}
private function get_block($str, $i){return $this->psubstr($str, $i, BLOCK_SIZE);}
private function x_enc($string, $key){
for($i=0; $i<=strlen($string)-1; $i++){
for($j=0; $j<=strlen($key)-1; $j++){
$string[$i] = $string[$i]^$key[$j];
}
if($i != 0 && $i != strlen($string)-1){
$string[$i] = $string[$i]^$string[$i-1]^$string[$i+1];
}
}
return $string;
}
private function x_dcd($string, $key){
for($i=strlen($string)-1; $i>=0; $i--){
if($i == 0 || $i == strlen($string)-1){
for($j=0; $j<=strlen($key)-1; $j++){
$string[$i] = $string[$i]^$key[$j];
}
}else{
$string[$i] = $this->x_enc($string[$i], $key)^$string[$i-1]^$string[$i+1];
}
}
return $string;
}
private function enc($str, $key){
$str = base64_encode($this->blockise($str));
for($i=0;$i<=strlen($str)-1;$i+=BLOCK_SIZE){
if($i == 0 || $i == (strlen($str)-BLOCK_SIZE)){
$repl = $this->x_enc($this->psubstr($str, $i, BLOCK_SIZE), $key);
$str = substr_replace($str, $repl, $i, BLOCK_SIZE);
}else{
if($i == BLOCK_SIZE){
$b = $this->x_enc($this->get_block($str, $i), $key);
}
$repl = $this->x_enc($this->get_block($str, $i+BLOCK_SIZE), $this->get_block($str, $i-BLOCK_SIZE));
$str = substr_replace($str, $repl, $i, BLOCK_SIZE);
}
}
$str .= $b;
return $str;
}
private function dcd($str, $key){
$b = $this->x_dcd($this->get_block($str, strlen($str)-BLOCK_SIZE), $key);
$str = substr($str, 0, strlen($str)-BLOCK_SIZE);
for($i=strlen($str)-BLOCK_SIZE;$i>=0;$i-=BLOCK_SIZE){
if($i == 0 || $i == (strlen($str)-BLOCK_SIZE)){
$repl = $this->x_dcd($this->psubstr($str, $i, BLOCK_SIZE), $key);
$str = substr_replace($str, $repl, $i, BLOCK_SIZE);
}else{
if($i == BLOCK_SIZE){
$str = substr_replace($str, $b, $i, BLOCK_SIZE);
}else{
$repl = $this->x_dcd($this->psubstr($str, $i-BLOCK_SIZE, BLOCK_SIZE), $this->psubstr($str, $i-(BLOCK_SIZE*2), BLOCK_SIZE));
$str = substr_replace($str, $repl, $i, BLOCK_SIZE);
}
}
}
return $this->dblockise(base64_decode($str));
}
public function encode($str, $key, $mode = ENC_BIN){
if($mode == ENC_BIN){
return $this->enc($this->enc($str, $key), md5($key));
}elseif($mode == ENC_DEC){
return $this->dec_encode($this->enc($this->enc($str, $key), md5($key)));
}
trigger_error("Invalid MODE SPecified", E_ERROR);
return false;
}
public function decode($str, $key, $mode = ENC_BIN){
if($mode == ENC_BIN){
return $this->dcd($this->dcd($str, md5($key)), $key);
}elseif($mode == ENC_DEC){
return $this->dcd($this->dcd($this->dec_decode($str), md5($key)),$key);
}
trigger_error("Invalid MODE SPecified", E_ERROR);
return false;
}
private function dec_decode($salted){
$key = array(0,1,2,3,4,5,6,7,8,9);$ret = '';
$val = $this->val;
$str = str_replace($val, $key, $salted);
for($i=0;$i<=strlen($str)-1;$i+=3){
$current_group = $str[$i].$str[$i+1].$str[$i+2];
$tmp_int = (int)($current_group);
$ret .= chr($tmp_int);
}
return base64_decode($ret);
}
private function dec_encode($salt){
$str = base64_encode($salt);$tmp = '';
for($i=0;$i<=strlen($str)-1;$i++){
if(ord($str[$i]) < 100){
if(ord($str[$i]) > 0 && ord($str[$i]) < 10){
$num = "00".ord($str[$i]);
}
elseif(ord($str[$i]) >= 10 && ord($str[$i]) < 100){
$num = "0".ord($str[$i]);
}
}
else{
$num = ord($str[$i]);
}
$tmp .= $num;
}
$key = array(0,1,2,3,4,5,6,7,8,9);
$val = $this->val;
$ret = str_replace($key, $val, $tmp);
return $ret;
}
}Code: Select all
<?php
$str = "1111111111111111111111111111111";
$key = "neel";
$crypto = new crytpo();
$e = $crypto->encode($str, $key);
echo "Encrypted \t[".$e."] Length \t".strlen($e)."\n\n";
$d = $crypto->decode($e, $key);
flush();//This is not needed at all.
echo "Decrypted \t[".$d."] Length ".strlen($d)."\n";
?>Output wrote:Encrypted [
P5q#Qqn_<1gvOkqno4QrjztwFeQ
*T)
bQQvi:j2<] Length 68
Decrypted [1111111111111111111111111111111] Length 31
Code: Select all
<?php
$str = "1111111111111111111111111111111";
$key = "neel";
$crypto = new crytpo();
$e = $crypto->encode($str, $key, ENC_DEC);
echo "Encrypted \t[".$e."] Length \t".strlen($e)."\n\n";
$d = $crypto->decode($e, $key, ENC_DEC);
flush();//This is not needed at all.
echo "Decrypted \t[".$d."] Length ".strlen($d)."\n";
?>ENC_DEC is an optional argument that again encrypts the encrypted string with 10 charactersOutput wrote:Encrypted [ -@+_ &% =+ @= =_ @* =% %% =* @@ -: =: == @ ++@ @_ --++-+ _ = -: %%+_ -=++%+ ++ @++ &+_ : =& : ++-+_ %=+ % :-+ @= @ @ +_+ %@++ ++_ &=+ +_ + * =: =_+ % =- =_ @ -:++- :* %=++ @& % %%+ @ =%++* =- -: @@ @: @: =_++_+ : =: =_ @@ @@ -- &% @ : %@ =&++_++* @@+ -++% -+] Length 276
Decrypted [1111111111111111111111111111111] Length 31
Code: Select all
$this->val = array(' ', '+', '_', '*', '&', ':', '-', '@', '=', '%');------------------------------ SECURITY --------------------------------------
See here we are encrypting 1111111111111111111111111111 a repetative character string
But see the encrypted String there is no repetation
If you use ENC_DEC the hacker will need to know 11 things
the keyword and that 10 Characters
Thats hardly possible.