<?php

function generateCodeword($l) {
	$codeword = "";
	for ($i = 0; $i < $l; $i++){
		$rand = rand(65, 116);
		if ($rand > 90) {
			$rand += 6;
		}
		$codeword .= chr($rand);
	}
	return $codeword;
}


function getLetterValue($char) {
	return ord($char) - 33;
}


function getNumberValue($nr) {
	return chr((($nr + 94) % 94) + 33);
}


function encodeChar($char, $offset) {
	return getNumberValue(getLetterValue($char) + getLetterValue($offset));
}


function decodeChar($char, $offset) {
	return getNumberValue(getLetterValue($char) - getLetterValue($offset));
}


function encode($codeword, $message) {
	$encrypted = "";
	for ($i = 0; $i < strlen($message); $i++) {
		$encrypted .= encodeChar(substr($message, $i, 1), substr($codeword, $i % strlen($codeword), 1));
	}
	return $encrypted;
}


function hideCodeword($codeword, $message) {
	$len = strlen($codeword);
	$hidden = "";
	for ($i = 0; $i < $len; $i++) { 
		$hidden .= substr($message, $i, 1);
		$hidden .= substr($codeword, $i, 1);
	}
	$hidden .= substr($message, $len, strlen($message) - $len - 1);
	$hidden .= chr(64 + $len);
	$hidden .= substr($message, -1);
	return $hidden;
}


function encrypt($message) {
	$cw = generateCodeword(rand(7, 14));
	return hideCodeword($cw, encode($cw, $message));
}


function decrypt($code) {
	$len = ord(substr($code, -2, 1)) - 64;
	$code = substr($code, 0, -2) . substr($code, -1);
	$codedMessage = "";
	$codeword = "";
	for ($i = 0; $i < (2 * $len); $i += 2) {
		$codedMessage .= substr($code, $i, 1);
		$codeword .= substr($code, $i + 1, 1);
	}
	$codedMessage .= substr($code, 2 * $len);
	$decrypted = "";
	for ($i = 0; $i < strlen($codedMessage); $i++) {
		$decrypted .= decodeChar(substr($codedMessage, $i, 1), substr($codeword, $i % strlen($codeword), 1));
	}
	return $decrypted;
}

?>


<?php

if(isset($_POST['encode'])) {
	$encode = $_POST['encode'];
	if (strlen($encode) < 16) {
		$decode = "An input string must at least be 16 characters...";
	} else {
		$decode = encrypt($encode);
	}
}

?>
<!DOCTYPE html>
<html>
	<head>
		<title>Encryption/Decryption tool</title>
		<meta author="Benjamin Ver"/>
		<!-- Kudos to 16yo Benjamin for this site and the idea for the encryption. -->
		<style type="text/css">
			.FFCG {	font-family: "Century Gothic"; }
			#decode { <?php if (strlen($encode) < 16) { echo  'color: red;'; } ?> }
		</style>

	</head>
	<body>
		<table align="center">
			<th colspan="2" class="FFCG">Encryption/Decryption tool</th>
			<tr>
				<td class="FFCG">Coderen:</td>
				<td class="FFCG">Decoderen:</td>
			</tr>
			<tr>
				<form action="decrypt.php" method="POST">
					<td><textarea name="encode" style="width: 500px; height: 150px" class="FFCG"><?php echo $encode; ?></textarea></td>
					<td><textarea id="decode" name="decode" style="width: 500px; height: 150px" class="FFCG" readonly><?php echo $decode; ?></textarea></td>
			</tr>
			<tr>
				<td colspan="2">
				<input type="submit" value="encode" class="FFCG"></td>
			</tr>
			</form>
		</table>

	</body>
</html>