<?php

class g_i_crypt
{


##################These are tools##################


//correct the octet
protected function fixoctet($bin){

	//if you dont put this value first in a var a error wil occur
	$len = (8 - strlen($bin));

	for($i = 0; $i < $len ; $i++){
	
		$bin = "0".$bin; 
	}
	

	return $bin;
}


//makes the key binary
protected function key2octet($key){


	for($o = 0; $o < $this->key_len; $o++){
	
		
		$key_temp = decbin(ord($key[$o]));
		$this->bin_key[$o] = self::fixoctet($key_temp);

	}
	
}


//makes the data binary
protected function data2octet($data){


	for($o = 0; $o < $this->data_len; $o++){
	
		
		$data_temp = decbin(ord($data[$o]));
		$this->bin_data[$o] = self::fixoctet($data_temp);
		

	}
	
}

//converts the binary data to readable
protected function octet2data($data){


	$this->output_data = "";
	
	while(list($key,$value) = each($data)){
		$this->output_data .= chr(bindec($value));
	}
	
}


//blends the two octets together
protected function blendoctet($octet1,$octet2){

	
	for($i = 0; $i < sizeof($octet1); $i++){
		
		$key_pos = $i % sizeof($this->bin_key);
		
		for($o = 0; $o < 8; $o++){
		
			if($octet2[$key_pos][$o] == "1" && $octet1[$i][$o] == "0"){
				$octet1[$i][$o] = "1";
				continue;
			}
			
			if($octet2[$key_pos][$o] == "1" && $octet1[$i][$o] == "1"){
				$octet1[$i][$o] = "0";
				continue;
			}
			
			if($octet2[$key_pos][$o] == "0" && $octet1[$i][$o] == "0"){
				$octet1[$i][$o] = "0";
				continue;
			}
			
			if($octet2[$key_pos][$o] == "0" && $octet1[$i][$o] == "1"){
				$octet1[$i][$o] = "1";
				continue;
			}
		}
	}
	
	$this->output_data = $octet1;
}


##################This is where its about##################


//encrypt the data
public function encrypt($data,$key){

	//data length
	$this->data_len = strlen($data);
	
	//key length
	$this->key_len = strlen($key);
	
	
	//makes the key binary
	self::key2octet($key);
	
	//makes the data binary
	self::data2octet($data);
	
	//blends the password with the data
	self::blendoctet($this->bin_data,$this->bin_key);
	
	//converts the binary data to readable data
	self::octet2data($this->output_data);
	
	//Hmm wondering what this does :P
	return $this->output_data;
	
}


//decrypt the data
public function decrypt($data,$key){

	return self::encrypt($data,$key);
	
}



//end of class
}

$class = new g_i_crypt();
$data = $class->encrypt("G-Interfaces.com","chessere");
echo $data."<br/>";
$data = $class->decrypt($data,"chessere");
echo $data."<br/>";

?>