<?php 
class PlateScrumble
{



const CO_PSTRING_RULE_LENGTH	=	39;//((57-48 + 1) + (90-65+1) + 2); // numeri + lettere maiuscole + []
const CO_PSTRING_RULE_FIRST_CHAR = 65;
const CO_PSTRING_RULE_LAST_CHAR = 90;
const CO_PSTRING_RULE_FIRST_NUM = 48;
const CO_PSTRING_RULE_LAST_NUM = 57;
const CO_PSTRING_RULE_OPEN_SQUARE_PARENTHESIS = 91;
const CO_PSTRING_RULE_CLOSE_SQUARE_PARENTHESIS = 93;
const CO_PSTRING_RULE_QUESTION_MARK				= 63;


	var $secretkey;
	var $ScrumbleRule;
	var $UnScrumbleRule;


function __construct()
{
	$this->Reset();
	

	$this->secretkey = '';
}
function Init($theSecretKey)
{
	$secretkey =strtoupper($theSecretKey);
	$this->secretkey= $secretkey;
}
function Crypt($Str)
{
	$Str=strtoupper($Str);

	if(strlen($this->secretkey) <= 0)
		return ($Str);

	$StrResult = "";
	

	for($n = 0; $n < strlen($Str); $n++)	
	{
	//	echo $this->secretkey;
		$KeyIndex = ($n%strlen($this->secretkey));
		//echo $KeyIndex;
	//	echo substr($this->secretkey, $KeyIndex, 1);
		
		$charSec = substr($this->secretkey, $KeyIndex, 1);
		$charStr = substr($Str, $n, 1);

	//	echo "->$n#$charSec#$charStr\n";
				
		$FirstIndex = $this->CharToIndex($charSec);
		$SecondIndex = $this->CharToIndex($charStr);
		
	//	echo "->$FirstIndex @ $SecondIndex";
		
	//	echo chr($this->ScrumbleRule[$this->CharToIndex($charSec)][$this->CharToIndex($charStr)]);
		
		$StrResult = $StrResult.chr($this->ScrumbleRule[$this->CharToIndex($charSec)][$this->CharToIndex($charStr)]);
	}
	
//	echo "----";

	return $StrResult;
}

function DeCrypt($Str)
{
	if(strlen($this->secretkey) <= 0)
		return ($Str);

	$StrResult = "";

	for($n = 0; $n < strlen($Str); $n++)	
	{
//		echo "\n";
		
		$charSec = substr($this->secretkey, ($n%strlen($this->secretkey)), 1);
		$charStr = substr($Str, $n, 1);
//		echo "->$n#$charSec#$charStr  ";
//		echo "->$this->CharToIndex($charSec)#$this->CharToIndex($charStr)  ";
		
		$FirstIndex = $this->CharToIndex($charSec);
		$SecondIndex = $this->CharToIndex($charStr);
		
//		echo "->$FirstIndex @ $SecondIndex";
//		echo "-> ".$this->UnScrumbleRule[$this->CharToIndex($charSec)][$this->CharToIndex($charStr)];
		
		$StrResult = $StrResult.($this->UnScrumbleRule[$this->CharToIndex($charSec)][$this->CharToIndex($charStr)]);
		
//		echo " $StrResult"; 
	}

	return $StrResult;//$StrResult;
}

function Reset()
{
	
	for($i = 0; $i < self::CO_PSTRING_RULE_LENGTH; $i++)
	{
//		echo "<p>";
		
		for($n = 0; $n < self::CO_PSTRING_RULE_LENGTH; $n++)
		{
		//	echo $i;
			$this->ScrumbleRule[$i][$n] = $this->IndexToChar((($n+$i) % self::CO_PSTRING_RULE_LENGTH));
	//		echo chr($this->ScrumbleRule[$i][$n]).' ';
		}
	
		//	echo "</p>";
		
	}

	for($i = 0; $i < self::CO_PSTRING_RULE_LENGTH; $i++)
	{
		for($n = 0; $n < self::CO_PSTRING_RULE_LENGTH; $n++)
		{
			//$this->UnScrumbleRule[$i][$this->CharToIndex(chr($this->ScrumbleRule[$i][$n]))] =  ($this->IndexToChar($n));
			$this->UnScrumbleRule[$i][$this->CharToIndex(chr($this->ScrumbleRule[$i][$n]))] =  chr($this->IndexToChar($n));
		}
	}
}


function CharToIndex($ValChar)
{
	$Val = ord($ValChar);
	
	if($Val >= self::CO_PSTRING_RULE_FIRST_CHAR && $Val <= self::CO_PSTRING_RULE_LAST_CHAR)
	{
		return ($Val - self::CO_PSTRING_RULE_FIRST_CHAR);
	}
	else if($Val >= self::CO_PSTRING_RULE_FIRST_NUM && $Val <= self::CO_PSTRING_RULE_LAST_NUM)
	{
		return ($Val - self::CO_PSTRING_RULE_FIRST_NUM + (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 1));
	}
	else if($Val == self::CO_PSTRING_RULE_OPEN_SQUARE_PARENTHESIS)
		return ((self::CO_PSTRING_RULE_LAST_NUM - self::CO_PSTRING_RULE_FIRST_NUM + 1) + (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 1));
	else if($Val == self::CO_PSTRING_RULE_CLOSE_SQUARE_PARENTHESIS)
		return ((self::CO_PSTRING_RULE_LAST_NUM - self::CO_PSTRING_RULE_FIRST_NUM + 1) + (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 2));
	else if($Val == self::CO_PSTRING_RULE_QUESTION_MARK)
		return ((self::CO_PSTRING_RULE_LAST_NUM - self::CO_PSTRING_RULE_FIRST_NUM + 1) + (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 3));
	

	return 0;
}

function IndexToChar($Val)
{
	if($Val >= 0 && $Val <= (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR))
	{
		return ($Val + self::CO_PSTRING_RULE_FIRST_CHAR);
	}
	else if($Val >= (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 1) && $Val <= ((self::CO_PSTRING_RULE_LAST_NUM - self::CO_PSTRING_RULE_FIRST_NUM) + (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 1)))
	{
		return ($Val - (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 1) + self::CO_PSTRING_RULE_FIRST_NUM);
	}
	else if($Val == ((self::CO_PSTRING_RULE_LAST_NUM - self::CO_PSTRING_RULE_FIRST_NUM + 1) + (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 1)))
		return self::CO_PSTRING_RULE_OPEN_SQUARE_PARENTHESIS;
	else if($Val == ((self::CO_PSTRING_RULE_LAST_NUM - self::CO_PSTRING_RULE_FIRST_NUM + 1) + (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 2)))
		return self::CO_PSTRING_RULE_CLOSE_SQUARE_PARENTHESIS;
	else if($Val == ((self::CO_PSTRING_RULE_LAST_NUM - self::CO_PSTRING_RULE_FIRST_NUM + 1) + (self::CO_PSTRING_RULE_LAST_CHAR - self::CO_PSTRING_RULE_FIRST_CHAR + 3)))
		return self::CO_PSTRING_RULE_QUESTION_MARK;
	

	return 0;
}
		
}
?> 