function _tr_pnum($s)
{
	/* sum the ascii values of each char */
	$ret = 0;
	for ( $i = 0; $i < strlen($s); $i++ )
	{
		$ret += ord($s[$i]) + $i;
	}
	/* bit shift ret 1 position to the left until ret exceeds PW_MIN_NUM */
	while ($ret < PW_MIN_NUM)
	{
		$ret = $ret << 1;
	}
	return $ret;
}

function _tr_crypt ( $instr, $pwstr, $len)
{
   $passnum = (int) ((((_tr_pnum($pwstr)/997) - 1) % 254 ) + 1);
   $pwlen = strlen($pwstr);

   for ( $i = $j = 0; $i < $len; $i++ )               /* process whole string */
   {
       $passnum = (int) ((($passnum + ( $i - $len )) - 1 ) % 254) + 1;
       $buff[0] = (ord($instr[$i]) ^ ($passnum ^ ord($pwstr[$j])) );      /* XOR 3 var's */
       $saida[$i] = ($buff[0] ? chr($buff[0]) : $instr[$i]);   /* if NULL return char*/
       $j = ( $j = $pwlen ? 0 : $j + 1 );    /* password que control variable */
   }
   $saida = implode("",$saida);

   return $saida;                      /* send back encrypted string */
}

function encrypt($string,$pass)
{
	$strlen = strlen($string);
	return _tr_crypt($string, $pass, $strlen);
}