<?php
/*
XECrypt Decrypter v.1.0
Developer: Brendan M.
Date: January 18, 2010
*/

if (empty($_GET['submit'])) {
?>
<html>
	<head>
		<title>XECrypt Decrypter</title>
	</head>
	<body style="font-family: Tahoma;">
		<h2>XECrypt Decrypter</h2><br />
		
		<form action="#" method="get">
			<textarea style="font-family: Tahoma;font-size: 14px;" name="text" rows="10" cols="30"></textarea><br />
			<input type="submit" name="submit" value="Decrypt" />
		</form>
	</body>
</html>
<?php
} else {
?>
<html>
	<head>
		<title>XECrypt Decrypter</title>
	</head>
	<body style="font-family: Tahoma;">
		<h2>XECrypt Decrypter</h2> <a href="index.php">(Go back)</a><br />
		<h3>Possible Translations:</h3><br />
<?php
	// Begin Decryption.
	$message = $_GET['text'];

	// Find each characters total value.
	$parray = explode(".", $message);
	$array = array();
	
	//Clean up the values
	foreach($parray as $value) {
		$string = trim($value);
		array_push($array, $string);
	}

	$values = array();
	$num = count($array);
	$i = 0;

	while ($i < $num) {
		$math1 = $i;
		$math2 = $i + 1;
		$math3 = $i + 2;
		
		$value1 = $array["$math1"];
		$value2 = $array["$math2"];
		$value3 = $array["$math3"];
		
		$total = $value1 + $value2 + $value3;
		array_push($values, $total);
		
		$i += 3;
	}
	
	$lvalue = $values['0'];
	
	$least = $lvalue - 126;
	$most = $lvalue - 32;
	$num = $least;
	$decryptions = array();
	
	while ($num <= $most) {
		$temp = array();
		
		foreach($values as $letter) {
			$char = $letter - $num;
			$chr = chr($char);
			array_push($temp, $chr);
		}
		$string = implode($temp);
		array_push($decryptions, $string);
		$num++;
	}
	
	$acceptable = 0;
	
	foreach($decryptions as $message) {
		$search1 = strpos($message, " a ");
		$search2 = strpos($message, " I ");
		$search3 = strpos($message, " of ");
		$search4 = strpos($message, " the ");
		$search5 = strpos($message, " so ");
		
		if (!empty($search1) || !empty($search2) || !empty($search3) || !empty($search4) || !empty($search5) || !empty($_GET['show']) && $_GET['show'] == "all") {
			echo $message . "<br /><br />";
			$acceptable++;
		}
	}
	
	if ($acceptable == 0) {
		echo ("<center><span style='color: red;font-weight: bold;'><i>No acceptable translations. (<a href='" . $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] . "&show=all'>Show All</a>)</i></span></center>");
	}
}
?>