Encryption | decryption
Based on the conversion of the system
?
Made by Huyang @2005-01-20| | 11-04-06
# #用n进位制到m进位制
# #0 ~9a-z, up to 36 in range
print ' <title> encryption decryption method </title> ';
Class carry
{
function Carry ($n, $m)
{
$this->n= $n;
$this->m= $m;
$this->chn[' 0 ']=0;
$this->chn[' 1 ']=1;
$this->chn[' 2 ']=2;
$this->chn[' 3 ']=3;
$this->chn[' 4 ']=4;
$this->chn[' 5 ']=5;
$this->chn[' 6 ']=6;
$this->chn[' 7 ']=7;
$this->chn[' 8 ']=8;
$this->chn[' 9 ']=9;
$this->chn[' A ']=10;
$this->chn[' B ']=11;
$this->chn[' C ']=12;
$this->chn[' D ']=13;
$this->chn[' E ']=14;
$this->chn[' F ']=15;
$this->chn[' G ']=16;
$this->chn[' H ']=17;
$this->chn[' I ']=18;
$this->chn[' J ']=19;
$this->chn[' K ']=20;
$this->chn[' L ']=21;
$this->chn[' M ']=22;
$this->chn[' N ']=23;
$this->chn[' O ']=24;
$this->chn[' P ']=25;
$this->chn[' Q ']=26;
$this->chn[' R ']=27;
$this->chn[' S ']=28;
$this->chn[' T ']=29;
$this->chn[' U ']=30;
$this->chn[' V ']=31;
$this->chn[' W ']=32;
$this->chn[' X ']=33;
$this->chn[' Y ']=34;
$this->chn[' Z ']=35;
$this->cn[0]= ' 0 ';
$this->cn[1]= ' 1 ';
$this->cn[2]= ' 2 ';
$this->cn[3]= ' 3 ';
$this->cn[4]= ' 4 ';
$this->cn[5]= ' 5 ';
$this->cn[6]= ' 6 ';
$this->cn[7]= ' 7 ';
$this->cn[8]= ' 8 ';
$this->cn[9]= ' 9 ';
$this->cn[10]= ' A ';
$this->cn[11]= ' B ';
$this->cn[12]= ' C ';
$this->cn[13]= ' D ';
$this->cn[14]= ' E ';
$this->cn[15]= ' F ';
$this->cn[16]= ' G ';
$this->cn[17]= ' H ';
$this->cn[18]= ' I ';
$this->cn[19]= ' J ';
$this->cn[20]= ' K ';
$this->cn[21]= ' L ';
$this->cn[22]= ' M ';
$this->cn[23]= ' N ';
$this->cn[24]= ' O ';
$this->cn[25]= ' P ';
$this->cn[26]= ' Q ';
$this->cn[27]= ' R ';
$this->cn[28]= ' S ';
$this->cn[29]= ' T ';
$this->cn[30]= ' U ';
$this->cn[31]= ' V ';
$this->cn[32]= ' W ';
$this->cn[33]= ' X ';
$this->cn[34]= ' Y ';
$this->cn[35]= ' Z ';
}
function Check1 ($a)
{
if (Ereg ("[0-9]", $a))
if ($a >=2)
if ($a <=36)
return true;
return false;
}
function Check2 ($a)
{
$la = 0;
for ($j =0; $j <strlen ($a); $j + +)
{
if ($this->chn[substr ($a, $j, 1)]>= $this->n)
$la = 1;
if ($la)
Break
}
if ($la)
return false;
Else
return true;
}
function Todec ($a)//$n-> Decimal
{
if ($this->n==10)
return $a;
Else
{
$a =strrev ($a);
$k = 0;
for ($i =0; $i <strlen ($a); $i + +)
{
$k + + $this->chn[substr ($a, $i, 1)]*pow ($this->n, $i);
}
return $k;
}
}
function Gethigh ($a)
{
if ($a <=1)
return 1;
Else
{
For ($i =0 $i <100; $i + +)
{
if (Pow ($this->m, $i) > $a)
Break
}
return $i;
}
}
function ToM ($a)/decimal-> $m
{
$a 1= $this->gethigh ($a);
$res = "";
for ($i =1; $i <= $a 1; $i + +)
{
$u = ($a-$a%pow ($this->m, ($a 1-$i))/(Pow ($this->m, ($a 1-$i)));
$a-= $u *pow ($this->m, ($a 1-$i));
$res. = $this->cn[$u];
}
return $res;
}
function get ($a)
{
if (! $this->n) $this->n=10;
if (! $this->m) $this->m=10;
if ($this->check1 ($this->n)) && ($this->check1 ($this->m))
{
if (Ereg ("[0-9a-z]", $a))
{
if ($this->check2 ($a))
{
return $this->tom ($this->todec ($a));
}
Else
return false;
}
Else
return false;
}
Else
return false;
}
}
Class Han
{
Function Han ()
{
For ($i =0 $i <10; $i + +)
{
$this->hu1[$i]= $i;
$this->hu2[$i]= $i;
}
For ($i =10 $i <36; $i + +)
{
$this->hu1[$i]=chr ($i +55);
$this->HU2[CHR ($i +55)]= $i;
}
For ($i =36 $i <62; $i + +)
{
$this->hu1[$i]=chr ($i +61);
$this->HU2[CHR ($i +61)]= $i;
}
$this->HU1[62]=CHR (42);
$this->HU1[63]=CHR (43);
$this->HU2[CHR (42)]=62;
$this->HU2[CHR (43)]=63;
}
function Tocode ($STR)//Converts a set of characters into code 0~65536
{
$huyang 1=new Carry (10,33);
$j = 0;
for ($i =0; $i <strlen ($STR); $i + +)
{
$p =ord (substr ($str, $i, 1));
if ($p >160)
{
$q =ord (substr ($str, + + $i, 1));
$p = $p *256+ $q;
}
Else
$p =255*256+ $p;
$ret. = $huyang 1->get ($p);
$j + +;
}
return $ret;
}
function GetCode ($STR)
{
$huyang =new Carry (33,10);
$res = "";
For ($i =0 $i < (strlen ($STR)/4); $i + +)
{
$a = $huyang->get (substr ($str, ($i *4), 4);
$a 2= $a% 256;
$a 1= ($a-$a 2)/256;
if ($a 1==255)
$res. =CHR ($a 2);
Else
$res. =CHR ($a 1). chr ($a 2);
}
return $res;
}
function encode ($STR)
{
$res = "";
$huyang =new Carry (35,7);
$huyang 1=new Carry (8,10);
$str 1= $this->tocode ($STR);
$k =ceil (strlen ($str 1)/3);
for ($i =0; $i < $k; $i + +)
{
$word = $this->hu1[rand (0,61)];
$res 1= $huyang 1->get ($huyang->get (substr ($str 1, ($i *3), 3));
$res 11= ($res 1-$res 1% (64*64))/(64*64);
$res 1-= $res 11*64*64;
$res 12= ($res 1-$res 1%64)/64;
$res 1-= $res 12*64;
$res 13= $res 1;
if ($i = = ($k-1))
{
if ($res 11!=0)
{
$res. = $this->hu1[$res]. $this->hu1[$res]. $this->hu1[$res]. $word;
}
ElseIf ($res 12!=0)
{
$res. = $this->hu1[$res]. $this->hu1[$res]. $word;
}
ElseIf ($res 13!=0)
{
$res. = $this->hu1[$res]. $word;
}
}
Else
$res. = $this->hu1[$res]. $this->hu1[$res]. $this->hu1[$res]. $word;
}
Return trim ($res);
}
function Discode ($STR)
{
$len =ceil (strlen ($STR)/4);
$res = "";
$a =strlen ($STR) -4* ($len-1);
for ($i =0; $i < $len; $i + +)
{
if ($i!= ($len-1))
{
$res 1=substr ($str, $i *4,1);
$res 2=substr ($str, ($i *4+1), 1);
$res 3=substr ($str, ($i *4+2), 1);
$res 11= $this->hu2[$res 1];
$res 12= $this->hu2[$res 2];
$res 13= $this->hu2[$res 3];
$res 14= $res 11*64*64+ $res 12*64+ $res 13;
$res. = "". $res 14;
}
Else
{
if ($a%4==0)
{
$res 1=substr ($str, $i *4,1);
$res 2=substr ($str, ($i *4+1), 1);
$res 3=substr ($str, ($i *4+2), 1);
$res 11= $this->hu2[$res 1];
$res 12= $this->hu2[$res 2];
$res 13= $this->hu2[$res 3];
$res 14= $res 11*64*64+ $res 12*64+ $res 13;
$res. = "". $res 14;
}
ElseIf ($a%4==2)
{
$res 1=substr ($str, $i *4,1);
$res 11= $this->hu2[$res 1];
$res 14= $res 11;
$res. = "". $res 14;
}
ElseIf ($a%4==3)
{
$res 1=substr ($str, $i *4,1);
$res 2=substr ($str, ($i *4+1), 1);
$res 11= $this->hu2[$res 1];
$res 12= $this->hu2[$res 2];
$res 14= $res 11*64+ $res 12;
$res. = "". $res 14;
}
}
}
Return trim ($res);
}
function decode ($STR)
{
$str = $this->discode ($STR);
$a =explode ("", $str);
$res = "";
$huyang =new Carry (7,35);
$huyang 1=new Carry (10,8);
for ($i =0; $i <count ($a); $i + +)
{
$res. = $huyang->get ($a [$i]);
if ($i = = (count ($a)-1))
$res. = $huyang->get ($huyang 1->get ($a [$i]));
Else
{
$b = $huyang->get ($huyang 1->get ($a [$i]));
if (strlen ($b) ==0)
$res. = "000";
ElseIf (strlen ($b) ==1)
$res. = "$b";
ElseIf (strlen ($b) ==2)
$res. = "0". $b;
Else
$res. = $b;
}
}
return $this->getcode ($res);
}
}
$s =microtime ();
$st =explode (", $s);
$st 1= $st [0];
$st 2= $st [1];
$huyang =new Han;
$str =$_post[' str '];
if (! $str) $str = "Please enter query statement!!";
$len =strlen ($STR);
if ($submit)
$a = $huyang->encode ($STR);
if ($submit 1)
$a = $huyang->decode ($STR);
$a =str_replace ("\", "\", $a);
$a =str_replace ("\" "," \ "", $a);
$a =str_replace ("\ \", "\"), $a);
$s =microtime ();
$st =explode (", $s);
$sta = $st [0];
$STB = $st [1];
$ss 1= $sta-$st 1;
$ss 2= $stb-$st 2;
$cus = $ss 1+ $ss 2;
$cus 1= $cus/$len;
?>
<form method= "POST" action= "carry.php" >
<table border= "1" cellpadding= "0" cellspacing= "0" width= ">"
<tr>
<TD width= "9%" height= "><b> time-consuming/word length-(average time consuming) </b></td>
<TD width= "91%" height= "><?echo $cus." /'. $len. '-('. $cus 1 ') ';? ></td>
</tr>
<tr>
<TD width= "9%" height= "><b>" original statement </b></td>
<TD width= "91%" height= "><textarea cols=" rows= "8" readonly ><?echo $str;? ></textarea></td>
</tr>
<tr>
<TD width= "9%" height= "><b> results </b></td>
<TD width= "91%" height= "><textarea cols=" rows= "8" readonly ><?echo $a;? ></textarea></td>
</tr>
</table>
<p><textarea rows= "8" name= "str" cols= "><?echo" $a;? ></textarea></p>
<p><input type= "Submit" value= "Encryption" name= "Submit" >
<input type= "Submit" value= "Decrypt" name= "Submit1" ></p>
</form>