<?php
function Byte($x)
{
if($x>255)
{
return $x % 256;
}
else
{
if($x<0)
{
return 256 + ($x % 256);
}
else
{
return $x;
}
}
}
function RoundD($int)
{
return Round($int-0.5);
}
function shr($x, $y)
{
for($i=0;$i<$y;$i++)
{
$x=RoundD($x/2);
}
return $x;
}
function RCxInitRaw($Key, $KeySize)
{
$L=0;
for($S=0;$S<256;$S++)
{
$RCx[0][$S] = $S;
$M[$S] = Byte($Key[$S % $KeySize] ^ $L);
$L = ($L + $M[$S]*257) % 2147483647 + 1;
}
$RCx[1] = 0;
$RCx[2] = 0;
$R = Byte($L);
$RCx[3] = Byte(shr($L, 8));
for($S=0;$S<256;$S++)
{
$R = Byte($R + $RCx[0][$S] + $M[$S]);
$T = $RCx[0][$S];
$RCx[0][$S] = $RCx[0][$R];
$RCx[0][$R] = $T;
}
return $RCx;
}
function RCxInit($Key)
{
for($i=0;$i<strlen($Key);$i++)
{
$ar[$i] = ord(substr($Key,$i,1));
}
return RCxInitRaw($ar,$i);
}
function RCxEncodeRawRaw($RCx, $S, $count)
{
for($C=0;$C<$count;$C++)
{
$RCx[1] = Byte($RCx[1] + 1);
$T = $RCx[0][$RCx[1]];
$RCx[2] = Byte($RCx[2] + $T);
$RCx[0][$RCx[1]] = $RCx[0][$RCx[2]] ^ $RCx[3];
$RCx[0][$RCx[2]] = Byte($T - $RCx[3]);
$T = Byte($T + $RCx[0][$RCx[1]]);
$K = $S[$C];
$O[$C] = $K ^ $RCx[0][$T];
$RCx[3] = $RCx[3] ^ $K;
}
$res[0] = $RCx;
$res[1] = $O;
return $res;
}
function RCxEncodeRaw($RCx, $val)
{
for($i=0;$i<strlen($val);$i++)
{
$ar[$i] = ord(substr($val,$i,1));
}
$ret=RCxEncodeRawRaw($RCx, $ar, $i);
$str='';
for($i=0;$i<strlen($val);$i++)
{
if($ret[1][$i]>15)
{
$str.=base_convert($ret[1][$i],10,16);
}
else
{
$str.='0'.base_convert($ret[1][$i],10,16);
}
}
return $str;
}
function RCxDecodeRawRaw($RCx, $S, $count)
{
for($C=0;$C<$count;$C++)
{
$RCx[1] = Byte($RCx[1] + 1);
$T = $RCx[0][$RCx[1]];
$RCx[2] = Byte($RCx[2] + $T);
$RCx[0][$RCx[1]] = $RCx[0][$RCx[2]] ^ $RCx[3];
$RCx[0][$RCx[2]] = Byte($T - $RCx[3]);
$T = Byte($T + $RCx[0][$RCx[1]]);
$K = $S[$C] ^ $RCx[0][$T];
$O[$C] = $K;
$RCx[3] = $RCx[3] ^ $K;
}
$res[0] = $RCx;
$res[1] = $O;
return $res;
}
function RCxDecodeRaw($RCx, $val, $ofs)
{
for($i=0;$i<strlen($val)/2;$i++)
{
$ar[$i]=base_convert(substr($val,$i*2,2),16,10);
}
$ret=RCxDecodeRawRaw($RCx, $ar, $i);
$val=$ret[1];
$str='';
for($j=$ofs;$j<$i;$j++)
{
$str.=chr($val[$j]);
}
return $str;
}
function RCxEncode($val, $key, $offs)
{
if($offs<0 || isset($offs)==false)
{
$offs=16;
}
$val=RandomString($offs).$val;
$RCx=RCxInit($key);
$ret=RCxEncodeRaw($RCx, $val);
return $ret;
}
function RCxDecode($val, $key, $offs)
{
if($offs<0 || isset($offs)==false)
{
$offs=16;
}
$RCx=RCxInit($key);
$ret=RCxDecodeRaw($RCx, $val, $offs);
return $ret;
}
function RandomString($len)
{
$rand='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$str='';
for($i=0;$i<$len;$i++)
{
$str.=substr($rand,rand(0,strlen($rand)-1),1);
}
return $str;
}
function Hex16ToHex32($hex)
{
$hex32='0123456789ABCDEFGHKLMNOPRSTUVWXZ';
$key='';
$hex=strtoupper($hex);
for($i=0;$i<strlen($hex)/5;$i++)
{
$t=strpos($hex32,substr($hex,$i*5+4,1))+strpos($hex32,substr($hex,$i*5+3,1))*16+strpos($hex32,substr($hex,$i*5+2,1))*16*16+strpos($hex32,substr($hex,$i*5+1,1))*16*16*16+strpos($hex32,substr($hex,$i*5,1))*16*16*16*16;
$res[1]=substr($hex32,RoundD($t/(32*32*32)),1);
$t%=32*32*32;
$res[2]=substr($hex32,RoundD($t/(32*32)),1);
$t%=32*32;
$res[3]=substr($hex32,RoundD($t/32),1);
$t%=32;
$res[4]=substr($hex32,$t,1);
$key.='-'.$res[1].$res[2].$res[3].$res[4];
}
return substr($key,1);
}
function Hex32ToHex16($hex)
{
$hex32='0123456789ABCDEFGHKLMNOPRSTUVWXZ';
$key='';
$hex=strtoupper($hex);
for($i=0;$i<strlen($hex)/5;$i++)
{
$t=strpos($hex32,substr($hex,$i*5+3,1))+strpos($hex32,substr($hex,$i*5+2,1))*32+strpos($hex32,substr($hex,$i*5+1,1))*32*32+strpos($hex32,substr($hex,$i*5,1))*32*32*32;
$res[1]=substr($hex32,RoundD($t/(16*16*16*16)),1);
$t%=16*16*16*16;
$res[2]=substr($hex32,RoundD($t/(16*16*16)),1);
$t%=16*16*16;
$res[3]=substr($hex32,RoundD($t/(16*16)),1);
$t%=16*16;
$res[4]=substr($hex32,RoundD($t/16),1);
$t%=16;
$res[5]=substr($hex32,$t,1);
$key.=$res[1].$res[2].$res[3].$res[4].$res[5];
}
return $key;
}
function RCxEncode32($val, $key, $offs)
{
$val=strlen($val).'|'.$val;
while((strlen($val)+$offs) % 5 != 0)
{
$val.='0';
}
return Hex16ToHex32(RCxEncode($val, $key, $offs));
}
function RCxDecode32($val, $key, $offs)
{
$val=RCxDecode(Hex32ToHex16($val), $key, $offs);
$i=0;
$c='';
while(substr($val,$i,1)!='|')
{
$c.=substr($val,$i,1);
$i++;
}
$i++;
return substr($val,$i,$c*1);
while(substr($val,strlen($val)-1,1)==chr(0))
{
$val=substr($val,0,strlen($val)-1);
}
return $val;
}
function Encode($value,$password)
{
return RCxEncode32($value,$password,12);
}
function Decode($value,$password)
{
return RCxDecode32($value,$password,12);
}
?>