#!/usr/bin/perl

#ALTERATIONS TO MAKE
#min size of plaintext
#len of key vs plaintext

#Cracking the Code
#Senior Capstone GUI
#Program to implement web interface
#Talks with C++

#HTML text
print "Content-type: text/html\n\n";

print <<TOP_of_HTML_Page;
<html>
	<head>
		<title>Martina's Senior Capstone Home Page</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
	</head>
	<body>
		<table width="100%" align="center" frame="box">
			<tr valign="bottom">
				<td>
					<a href="index.html"><img src="h10.jpg" border="0" width="300px"></a>
				</td>
				<td valign="bottom">
					<table width="100%">
						<tr>
							<td align="right">
								<a href="index.html"><img src="h11.jpg" border="0"></a>
							</td>
							<td align="right">
								<a href="definition.html"><img src="h2.jpg" border="0"></a>
							</td>
							<td align="right">
								<a href="journal.html"><img src="h3.jpg" border="0"></a>
							</td>
							<td align="right">
								<a href="timeline.html"><img src="h4.jpg" border="0"></a>
							</td>
							<td>
								<a href="detail.html"><img src="h5.jpg" border="0"></a>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td align="center" width="100%" colspan="2">
					<img src="line.jpg" border="0" align="top" width="100%">
				</td>
			</tr>
			<tr>
				<td>
					<img src="details.jpg" border="0">
				</td>
				<td bgcolor="#D0CDF8">
					<table cellpadding="4" cellspacing="4" bgcolor="#D0CDF8" border="1">
						<tr>
							<td align="center"><a href="tutorial.html" style="text-decoration: none">Cryptography 
									Tutorial</a></td>
							<td align="center"><a href="CrackCode.pl" style="text-decoration: none">Try it Now!</a></td>
							<td align="center"><a href="analysis.html" style="text-decoration: none">Algorithm 
									Analysis</a></td>
							<td align="center"><a href="extensions.html" style="text-decoration: none">Future 
									Extensions</a></td>
							<td align="center"><a href="source.html" style="text-decoration: none">Source Code</a></td>
							<td align="center"><a href="sources.html" style="text-decoration: none">Sources</a></td>
						</tr>
					</table>
				</td>
			</tr>
		<tr bgcolor="#DAD9BD" width="100%" height="200px">
			<td colspan="2">
TOP_of_HTML_Page


##################################################################
##################################################################
###The following are all of the javascript functions
##################################################################
##################################################################
print <<JAVASTUFF;
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
function TextOptions()
{
	if (document.PickText.User_Entered.checked)
		{document.PickText.action = "CrackCode.pl?0.1";}
	else if (document.PickText.Auto_Entered.checked)
		{document.PickText.action = "CrackCode.pl?0.2";}

	document.PickText.submit();
}

function CheckText()
{
	if (document.EnterText.PlainText.value.length < (4*document.EnterText.Key.value.length))
	{
		alert ("Your plaintext is not long enough");
		document.EnterText.PlainText.focus();
		return false;
	}
	if (document.EnterText.Key.value.length == 0)
	{
		alert ("Please enter a key to encrypt with.");
		document.EnterText.Key.focus();
		return false;
	}
	return true;
}

	
function CheckSelect()
{
	var i;
	//set flag to false
	var choice = false;
	
	// Loop from zero to the one minus the number of radio button selections
	for (var i = 0; i < document.PickText.TextChoice.length; i++)
	{
		
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (document.PickText.TextChoice[i].checked)
		{
			choice = true; 
		}
	}

	if (!choice)
	{
		// no selections made 
		alert("You must select a text");
		return (false);
	}

	if (document.PickText.Key.value.length == 0)
	{
		alert ("Please enter a key to encrypt with.");
		document.PickText.Key.focus();
		return false;
	}
	return true;
}

function CheckSelect2()
{
	var i;
	//set flag to false
	var choice = false;
	// Loop from zero to the one minus the number of radio button selections
	for (i = 0; i < document.LengthAlgorithm.LengthChoice.length; i++)
	{
		
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (document.LengthAlgorithm.LengthChoice[i].checked)
		{
			choice = true; 
		}
	}

	if (!choice)
	{
		// no selections made 
		alert("You must select an algorithm");
		return (false);
	}
	return (true);
}
function CheckSelect3()
{
	var i;
	//set flag to false
	var choice = false;
	// Loop from zero to the one minus the number of radio button selections
	for (i = 0; i < document.PlainAlgorithm.PlainChoice.length; i++)
	{
		
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (document.PlainAlgorithm.PlainChoice[i].checked)
		{
			choice = true; 
		}
	}

	if (!choice)
	{
		// no selections made 
		alert("You must select an algorithm");
		return (false);
	}
	return (true);
}

function LengthVer()
{
	if (document.EnterLength.KeyLength.value=="")
	{
		alert("You must enter a length to continue!");
		return (false);
	}
	return (true);
}
		

</SCRIPT>
JAVASTUFF



@params = @ARGV; #this contains the username as well as option data
if (scalar (@params) == 0)
{
	#User just got to page, ask for text to cipher
	Intro();

}
else
{
	#set the library environment variable for user unkown so that the cpp program and include its libraries
	$ENV{'LD_LIBRARY_PATH'} = '/usr/local/lib';
	#arguments to handle
	#task is the flag as to which choice/page the user is on
	$task = $params[0];	
	#now get any data that was passed in from a form
	read(STDIN, $arguments, $ENV{'CONTENT_LENGTH'});
	
	if ($task eq "0.1")
	{
		#user wishes to enter their own plaintext
		Enter_Text();
		
	}
	elsif ($task eq "0.2")
	{
		#user wishes to use an existing example
		Select_Text();
	}
	elsif ($task eq "1.0")
	{
		#allow user to enter plaintext and key, and then encrypt
		Encrypt_Usr($arguments);
	}
	elsif($task eq "1.1")
	{
		#allow usr to enter key, then encrypt
		Encrypt_Auto($arguments);
	}
	elsif($task eq "2.0")
	{
		#Show options for finding the key length
		Length_Alg();
	}
	elsif($task eq "2.1")
	{
		#Find the key length
		Find_Length($arguments);
	}
	elsif($task eq "3.0")
	{
		#Show options for finding the plain text
		Plain_Alg($arguments);
	}
	elsif($task eq "3.1")
	{
		#Find the plaintext
		Find_Plain($arguments);
	}
	elsif($task eq "4.0")
	{
		#Decrypt the message usinig the user provided key
		Decrypt($arguments);
	}
	else
	{
		#user tried to get to a page by typing a cmd line argument
		print "PLEASE FOLLOW THE LINKS AND DO EVERY STEP OF THE PROGRAM, THANK YOU!";
	}
}

		
print <<BOTTOM_of_HTML_Page;			
			</td>
		</tr>
		<tr>
			<td align="left" width="100%" colspan="2" height="10%">
				<img src="bline.jpg" border="0" valign="top" align="center" width="100%" height="20%">
			</td>
		</tr>
		<tr>
			<td>
				<table border="0">
					<tr>
						<td>
							<a href="philosophy.html"><img src="foot1.jpg" border="0"></a>
						</td>
						<td>
							<a href="Resume.pdf"><img src="foot2.jpg" border="0"></a>
						</td>
						<td>
							<a href="career.html"><img src="foot3.jpg" border="0"></a>
						</td>
						<td>
							<a href="thank.html"><img src="foot4.jpg" border="0"></a>
						</td>
					</tr>
				</table>
			</td>
			<td>
				<a href="http://www.google.com"><img src="foot5.jpg" border="0" align="right"></a>
			</td>
		</tr>
	</table>
	</body>
</html>
BOTTOM_of_HTML_Page






##################################################################
##################################################################
##Function: Intro
##Purpose:  Displays the first screen of Cracking the Code program, allowing the user to select if they wish to
##		enter their own text or select an exisiting example, and then asks for the text to encrypt and the 
##		key to encrypt with.  Will do checking to ensure the key length < text length
##Entry Conditions:  None
##Exit Conditions:  Will reload the same page, but with the argument of 0.1 or 0.2 to signal the program to 
##			the user choice
##Special:  Auto submit on form
##Globals:  None.
##Called By: Main
##################################################################
##################################################################
sub Intro()
{
	print <<TEXTBLOCK1;
	<table>
		<tr>
			<td>
				<font style="FONT-SIZE: 13pt; FONT-FAMILY: 'Calisto MT'">The following screens will 
				take demonstrate a full encryption process, allowing you to enter the plaintext 
				and the key. After a cipher text is obtained, the key will be "thrown out"; you 
				can be assured that the key you enter will not be used in the decryption of the 
				text. Then you can select which algorithms to use on the cipher text to Crack 
				the Code. As you navigate the program, you will see that you can click on 
				various words to get a definition or description of the algorithm. I hope you 
				have as much fun using the program as I had making it! </font>
			</td>
		</tr>
		<tr>
			<td><br>
				<br>
			</td>
		</tr>
		<tr>
			<td>
				<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">
				<form name="PickText" action="CrackCode.pl" method="post" onclick="TextOptions()">
					<input type="radio" name="User_Entered"> Enter my own plaintext<br>
					<input type="radio" name="Auto_Entered"> Use an existing example<br>
				</form>
				</font>
			</td>
		</tr>
	</table>
TEXTBLOCK1
}


##################################################################
##################################################################
##Function: Select_Text
##Purpose:  Allow the user to select a program provided plaintext and enter their own key 
##Entry Conditions:  None
##Exit Conditions:  Will reload the same page, but with the argument of 1.1  to signal the program to encode text
##			   and move to next step
##Globals:  None.
##Called By: Main
##################################################################
##################################################################
sub Select_Text()
{
print <<TEXTBLOCK2;
	<table>
		<tr>
			<td>
				<font style="FONT-SIZE: 16pt; FONT-FAMILY: 'Calisto MT'">
				Select a text from the following:
				</font>
			</td>
		</tr>
		<tr>
			<td>
				<table cellspacing="5" cellpadding="10">
					<form name="PickText" action="CrackCode.pl?1.1" method="post" onsubmit="return CheckSelect()">
				<tr>
					<td>
						<input type="radio" name="TextChoice" value="1">
						<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">Martin Luther King, Jr.'s "I Have a Dream"</font>
					</td>
					<td>
						<input type="radio" name="TextChoice" value="2">
						<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">Declaration of Independence</font>
					</td>
					<td></td>
				</tr>
				<tr>
					<td>
						<input type="radio" name="TextChoice" value="3">
						<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">John F. Kennedy's Inaugural Address</font>
					</td>
					<td>
						<input type="radio" name="TextChoice" value="4">
						<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">Abbott and Castello's <i>Who's on First?</i></font>
					</td>
					<td></td>
				</tr>
				<tr>
					<td>
						<input type="radio" name="TextChoice" value="5">
						<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">Paper on Religious Dynamic in Presidential Elections</font>
					</td>
					<td>
						<input type="radio" name="TextChoice" value="6">
						<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">
						<i>The Tibetan Dilemma: Is Full Independence Plausible for Tibet?</i></font>
					</td>
					<td></td>
				</tr>
				<tr><td><br><br></td></tr>
				<tr valign="top">
					<td colspan="3">
						<font style="FONT-SIZE: 16pt; FONT-FAMILY: 'Calisto MT'">
						Enter the key to use for encryption: 
						</font>
						<textarea name="Key" cols="120" rows="5" wrap="soft"></textarea>
					</td>
				</tr>
				<tr><td><br></td></tr>
				<tr>
					<td>
						<input type="submit" value="Submit">
					</td>
				</tr>
				</form>
				</table>
			</td>
		</tr>
	</table>
TEXTBLOCK2
}
	
	
##################################################################
##################################################################
##Function: Enter_Text
##Purpose:  Allow the user to enter their own plaintext and key 
##Entry Conditions:  None
##Exit Conditions:  Will reload the same page, but with the argument of 1.0 to signal the program to encode text
##			   and move to next step
##Globals:  None.
##Called By: Main
##################################################################
##################################################################
sub Enter_Text()
{
print <<TEXTBLOCK3;
	<table>
		<tr valign="top">
			<td>
				<form name="EnterText" action="CrackCode.pl?1.0" method="post" onsubmit="return CheckText()">
					<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">Enter the plain text to encrypt: </font>
			</td>
			<td>
					<textarea name="PlainText" cols="120" rows="15" warp="soft"></textarea> 
			</td>
		</tr>
		<tr valign="top">
			<td>
					<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">Enter the key to use for encryption:</font>
			</td>
			<td>
					<textarea name="Key" cols="120" rows="5" wrap="soft"></textarea>
			</td>
		</tr>
		<tr>
			<td></td><td align="center">
					<input type="submit" value=" Submit ">
			</td>
		</tr>
				</form>
	</table>
TEXTBLOCK3
}

##################################################################
##################################################################
##Function: Encrypt_Usr
##Purpose:  Encrypt the user entered txt using the user entered key and display the ciphertext
##Entry Conditions:  The key and text from the textareas on the webpage, found in $arguments
##Exit Conditions:  Will reload the same page, but with the argument of 2.0 to signal the program to begin decryption
##Globals:  None.
##Called By: Main
##################################################################
##################################################################
sub Encrypt_Usr()
{
	my $args = $_[0];
	#parse on & to split the plain text from key
	@temp = split /&/,$arguments;
	#isolate plaintext and key
	@pltxt = split /=/,$temp[0];
	@ktxt = split /=/, $temp[1];
	#take out hex chars and restore ascii characters
	$pltxt[1] =~ s/%([0-9A-F][0-9A-F])/pack("c",hex($1))/ge;
	$pltxt[1] =~ s/\+/ /g;
	$ktxt[1] =~ s/%([0-9A-F][0-9A-F])/pack("c",hex($1))/ge;
	$ktxt[1] =~ s/\+/ /g;
	open(fout, ">plaintext.txt") or die print "Cannot open file to write plaintext.\n";
	print fout $pltxt[1];
	close(fout);
	open(fout, ">key.txt") or die print "Cannot open file to write the key.\n";
	print fout $ktxt[1];
	close(fout);
	Encrypt();	
}
##################################################################
##################################################################
##Function: Encrypt_Auto
##Purpose:  Encrypt the user selected txt using the user entered key and display the ciphertext
##Entry Conditions:  The key ift from the textarea on the webpage, found in $arguments
##Exit Conditions:  Will reload the same page, but with the argument of 2.0 to signal the program to begin decryption
##Globals:  None.
##Called By: Main
##################################################################
##################################################################
sub Encrypt_Auto()
{
	my $args = $_[0];
	#print $args;
	#parse on & to split the plain text from key
	@temp = split /&/,$arguments;
	#isolate plaintext choice and key
	@pltxt = split /=/,$temp[0];
	@ktxt = split /=/, $temp[1];
	#take out hex chars and restore ascii characters
	$ktxt[1] =~ s/%([0-9A-F][0-9A-F])/pack("c",hex($1))/ge;
	$ktxt[1] =~ s/\+/ /g;
	
	#OPEN APPROPRIATE FILE
	if ($pltxt[1] eq '1')
	{
		open(TRSF, "<mlk.txt") or die print "Cound not find the file to copy!\n";
		
	}
	elsif ($pltxt[1] eq '2')
	{
		open(TRSF, "<declaration.txt") or die print "Cound not find the file to copy!\n";
	}
	elsif($pltxt[1] eq '3')
	{
		open(TRSF, "<kennedy.txt") or die print "Cound not find the file to copy!\n";
	}
	elsif($pltxt[1] eq '4')
	{
		open(TRSF, "<AbbottCastello.txt") or die print "Cound not find the file to copy!\n";
	}
	elsif($pltxt[1] eq '5')
	{
		open(TRSF, "<ReligionPaper.txt") or die print "Cound not find the file to copy!\n";
	}
	elsif($pltxt[1] eq '6')
	{
		open(TRSF, "<Tibet.txt") or die print "Cound not find the file to copy!\n";
	}
	
	my @file = <TRSF>;
	close(TRSF);
	open(fout, ">plaintext.txt") or die print "Could not open plain text file!\n";
	foreach(@file)
	{
		print fout $_;
	}
	close (fout);
	open(fout, ">key.txt") or die print "Cannot open file to write the key.\n";
	print fout $ktxt[1];
	close(fout);
	Encrypt();
}

##################################################################
##################################################################
##Function: Encrypt
##Purpose: Call the C++ program to encrypt the text in plaintext.txt with the key in key.txt and display resulting screen
##Entry Conditions:  plaintext.txt and key.txt must have valid data
##Exit Conditions:  Will reload the same page, but with the argument of 2.0 to signal the program to begin decryption
##Globals:  None.
##Called By: Encrypt_Usr
##################################################################
##################################################################
sub Encrypt()
{
	#call to cpp program to encrypt code
	$out= `CrackCode.cgi 0 plaintext.txt key.txt`;
	print "$out";
	open(CIPHER, "<cipher.txt")or die print "Cannot open cipher file for reading.\n";
	my @data = <CIPHER>;
	close(CIPHER);
	open(PLTXT, "<plaintext.txt")or die print "Cannot open plaintext file for reading.\n";
	my @ptxt = <PLTXT>;
	close(PLTXT);
	open(KETXT, "<key.txt")or die print "Cannot open key file for reading.\n";
	my @ktxt = <KETXT>;
	close(KETXT);	
	
	
	print <<TEXTBLOCK4;
	<table width="100%">
		<tr>
			<td>
				<font style="FONT-SIZE: 20pt; FONT-FAMILY: 'Calisto MT'">
				Your plain text has been encrypted!<br><br>
				</font>
			</td>
			<td align="right">
				<a href="CrackCode.pl?2.0" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">
				Click Here to Throw Away the Key and Begin Decryption Process</a>
			</td>			
		</tr>
		<tr><td></td></tr>
		<tr>
			<td colspan="2">
				<font style="FONT-SIZE: 18pt; FONT-FAMILY: 'Calisto MT'">
				The resulting cipher text is:<br>
				<font style="FONT-SIZE: 12pt; FONT-FAMILY: 'Calisto MT'">
TEXTBLOCK4
		my $temp = $data[0]; 
			for($h=0; $h < length($temp); $h+=80)
			{
				print substr($temp, $h, 80);
				print "<br>";
			}

print <<TEXTBLOCK5;
				</font>
			</td>
		</tr>
		<tr>
			<td><br><br>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<font style="FONT-SIZE: 18pt; FONT-FAMILY: 'Calisto MT'">
				The plaintext you encrypted was:<br>
				</font>
				<font style="FONT-SIZE: 12pt; FONT-FAMILY: 'Calisto MT'">
TEXTBLOCK5
			foreach(@ptxt)
			{
				print "$_<br>";
			}
print <<TEXTBLOCK6;
		</font>
			</td>
		</tr>
		<tr>
			<td><br><br>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<font style="FONT-SIZE: 18pt; FONT-FAMILY: 'Calisto MT'" >
				The key you used for encryption was:<br>
				</font>
				<font style="FONT-SIZE: 12pt; FONT-FAMILY: 'Calisto MT'">
TEXTBLOCK6
			foreach(@ktxt)
			{
				print "$_<br>";
			}
print <<TEXTBLOCK7;
			</font>
			</td>
		</tr>
		<tr>
			<td colspan="2">
			<br>
				<a href="CrackCode.pl?2.0" style="FONT-SIZE: 20pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">
				Click Here to Throw Away the Key and Begin Decryption Process</a>
			</td>
		</tr>
	</table>
TEXTBLOCK7
}
 
 
##################################################################
##################################################################
##Function: Length_Alg()
##Purpose: Allow the user to select which algorithm they wish to use to find the key length
##Entry Conditions:  None
##Exit Conditions:  Will reload the same page, but with the argument of 2.1 to signal the program to begin finding
##			key length
##Globals:  None.
##Called By: Main
##################################################################
################################################################## 
sub Length_Alg()
{
print <<TEXTBLOCK8;
	<table>
		<tr>
			<td><font style="FONT-SIZE: 16pt; FONT-FAMILY: 'Calisto MT'">
				Please select the algorithm you wish to use to find the key length.  You can view the algorithms by following the links.
			</font></td>
		</tr>
		<tr>
			<td>
			<table cellspacing=5" cellpadding="10">
					<form name="LengthAlgorithm" action="CrackCode.pl?2.1" method="post" onsubmit="return CheckSelect2()">
				<tr>
					<td>
						<input type="radio" name="LengthChoice" value="1">
						<a target="new" href="PlugIn.html" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">"Plug it in"</a>
					</td>
					<td>
						<input type="radio" name="LengthChoice" value="2">
						<a target="new" href="ShiftCount.html" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">"Shift and Count"</a>
					</td>
				</tr>
				<tr>
					<td>
						<input type="radio" name="LengthChoice" value="3">
						<a target="new" href="Friedman.html" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">Friedman Test</a>
					</td>
					<td>
						<input type="radio" name="LengthChoice" value="4">
						<a target="new" href="Kasiski.html" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">Kasiski Test</a>
					</td>
				</tr>
				<tr>
					<td>
						<input type="radio" name="LengthChoice" value="5">
						<a target="new" href="AllLengths.html" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">Combine All Algorithms</a>
					</td>
				</tr>
				<tr>
					<td>
						<input type="submit" value="Submit">
					</td>
				</tr>
				<tr>
					<td colspan="2"><br><br><br>
					Please be patient with the loading time required by the following page.  Depending on the method you chose to 
					find the key length, the computing time required may be significant. 
					</td>
				</tr>
				</form>
			</table>
			</td>
		</tr>
	</table>
TEXTBLOCK8
}


##################################################################
##################################################################
##Function: Find_Length
##Purpose: Call the C++ program to find the key length of cipher.txt and display resulting screen, allow usr to select 
##		the length to use
##Entry Conditions:  cipher.txt must have valid data
##Exit Conditions:  Will reload the same page, but with the argument of 3.0 to signal the program to begin finding 
##				plaintext
##Globals:  None.
##Called By: Main
##################################################################
##################################################################
sub Find_Length()
{
	my $args =$_[0];
	#get the user's algorithm choice  isolated from description
	my @temp = split /=/,$args;
	#$temp[1] holds the parameter to the C++ program - options 1 - 5 matched exactly between perl and c++ progs
	#call to cpp program to encrypt code
	$out= `CrackCode.cgi $temp[1] cipher.txt`;
	print "$out";
	open(KEYLEN, "<output.txt")or die print "Cannot open output file for reading.\n";
	my @data = <KEYLEN>;
	close(KEYLEN);	
	print <<TEXTBLOCK9;
	<table width="100%">
		<tr>
			<td>
				<font style="FONT-SIZE: 20pt; FONT-FAMILY: 'Calisto MT'">
				Finished Computing Key Length...<br><br>
				</font>
			</td>		
		</tr>
		<tr><td></td></tr>
		<tr>
TEXTBLOCK9

	my $dat = $data[0];

	if (substr($dat, 0, 7) eq "!ERROR!" || substr($dat, 0, 1) eq "0")
	{
	print <<TEXTBLOCK10;
			<td>
				<font style="FONT-SIZE: 20pt; FONT-FAMILY: 'Calisto MT'">  
				Unfortunately, the method you choose could not compute an accurate key length
				and an error resulted.  Please select another method to find the key length, or
				manually enter the keylength you wish to proceed with below.
				</font>
			</td>
		</tr>
TEXTBLOCK10
	}
	elsif ((substr($dat, 0, 10) eq "!MULTIPLE!") && ($temp[1] eq "4"))
	{
		#User chose to do Kasiski Test and it resulted in multiple key lengths
	print <<TEXTBLOCK11;
			<td>
				<font style="FONT-SIZE: 20pt; FONT-FAMILY: 'Calisto MT'">  
				However, the Kasiski Test resulted in multiple key lengths.  These lengths
				are printed below.  You may choose to select another method, or manually enter
				the keylength you wish to proceed with below.  I would suggest that you use
				a keylength listed; more specifically, if a particular number appears that is
				divisible by a majority of the numbers listed, select this number.
				<br><br>
				</font>
			</td>
		</tr>
		<tr>
			<td>
TEXTBLOCK11
		$t = $data[1];
		print "$t, ";
		for ($h = 2; $h < scalar(@data); $h++)
		{
			if ($t != $data[$h])
			{
			   #unique value, so print it
				$t = $data[$h];
				print "$t, ";
			}
		}
		print "</td></tr>";
	}
	elsif($temp[1] <= 4)
	{
		#Valid data resulted, just print out the keylength
	print <<TEXTBLOCK12;
		<td>
			<font style="FONT-SIZE: 20pt; FONT-FAMILY: 'Calisto MT'"> 
			The probable key length is:&nbsp
TEXTBLOCK12
		print $dat;
		print "<br><br></font></td></tr>"
	}
	elsif($temp[1] == 5)
	{
	print <<TEXTBLOCK13;
		<td>
			<font style="FONT-SIZE: 20pt; FONT-FAMILY: 'Calisto MT'"> 
			The results of all tests are as follows:<br><br>
TEXTBLOCK13
		#user selected to use all methods
		for ($h = 0; $h < scalar(@data); $h++)
		{
			if(!(substr($data[$h], 0, 7) eq "Kasiski"))
			{
				print "$data[$h] <br>"
			}
			else
			{
				#Kasiski Test resulted in multiple key lengths
	print <<TEXTBLOCK14;
				<br><br>The Kasiski Test resulted in multiple key lengths.  These lengths
				are printed below.  I would suggest that you use either a length listed above,
				or select a length below that is divisible by a majority of the numbers listed.
				<br><br>
TEXTBLOCK14
			}
		}#end for
		print "<br><br></font></td></tr>"
	}
	
	print <<TEXTBLOCK15;
	<tr>
		<td><br><br>
		<form name="EnterLength" action="CrackCode.pl?3.0" method="post" onsubmit="return LengthVer()">
			<font style="FONT-SIZE: 16pt; FONT-FAMILY: 'Calisto MT'">
			Enter the key length you wish to use to continue decryption process: <br><br>
			</font>
			<input type="text" name="KeyLength" size="4" maxsize="8"></textarea>
			<br>
			<br>
			<input type="submit" value="Submit">
		</form>
		</td>
	</tr>
	<tr>
		<td>
		<br><br>
		<a href="CrackCode.pl?2.0" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'; text-decoration: none">
		Or click here to select a different algorithm for finding the key length.</a>
		</td>
	</tr>
	</table>
TEXTBLOCK15
}

##################################################################
##################################################################
##Function: Key_Alg()
##Purpose: Allow the user to select which algorithm they wish to use to find the plain text
##Entry Conditions:  None
##Exit Conditions:  Will reload the same page, but with the argument of 2.1 to signal the program to begin finding
##			key length
##Globals:  None.
##Called By: Main
##################################################################
################################################################## 
sub Plain_Alg()
{
	my $args =$_[0];
	#get the user's key length  isolated from description
	my @temp = split /=/,$args;
	
	print <<TEXTBLOCK16;
	<table>
		<tr>
			<td><font style="FONT-SIZE: 16pt; FONT-FAMILY: 'Calisto MT'">
				Please select the algorithm you wish to use to find the plain text.  You can view the algorithms by following the links.
			</font></td>
		</tr>
		<tr>
			<td>
			<table cellspacing=5" cellpadding="10">
					<form name="PlainAlgorithm" action="CrackCode.pl?3.1" method="post" onsubmit="return CheckSelect3()">
TEXTBLOCK16
				print "<INPUT TYPE=HIDDEN NAME=\"KeyLength\" value=$temp[1]>"; #send the key length along
	print <<TEXTBLOCK161;
				<tr>
					<td>
						<input type="radio" name="PlainChoice" value="1">
						<a target="new" href="Frequency.html" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">"Basic Frequency Analysis"</a>
					</td>
				</tr>
				<tr>
					<td>
						<input type="radio" name="PlainChoice" value="2">
						<a target="new" href="Permute.html" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">"Permute through All Shifts"</a>
					</td>
				</tr>
				<tr>
					<td>
						<input type="radio" name="PlainChoice" value="3">
						<a target="new" href="Relative.html" style="FONT-SIZE: 16pt; FONT-FAMLY: 'Calisto MT'" style="text-decoration: none">Find Relative Shifts between Key Letters</a>
					</td>
				</tr>
				<tr><td><br><br></td></tr>
				<tr>
					<td>
						<input type="submit" value="Submit">
					</td>
				</tr>
				</form>
			</table>
			</td>
		</tr>
	</table>
TEXTBLOCK161
}

##################################################################
##################################################################
##Function: Find_Plain
##Purpose: Call the C++ program to find the plain text/key of cipher.txt and display resulting screen, allow usr to select 
##		the key to use if they chose Find Relative Shifts Algorithm
##Entry Conditions:  cipher.txt must have valid data
##Exit Conditions:  Will reload the same page, but with the argument of 4.0 to signal the program to display resulting
##				plaintext IF the user chose Find Relative Shifts Algorithm ELSE show plaintext and key used
##				and provide link to try it again
##Globals:  None.
##Called By: Main
##################################################################
##################################################################
sub Find_Plain()
{
	my $args =$_[0];
	#split up the args
	my @temp = split /&/,$args;
	#split keylength from description
	my @keylen = split /=/,$temp[0];
	my $length = $keylen[1];
	#split algorithm choice from description
	my @choice = split/=/,$temp[1];
	my $algorithm = $choice[1];
	$algorithm = $algorithm + 5; #adjust to C++ program parameter requirements
	#call to cpp program to decrypt code
	$out= `CrackCode.cgi $algorithm  cipher.txt $length`;
	open(KES, "<output.txt")or die print "Cannot open output file for reading.\n";	
	read(KES, $buff, 7);
	if ($buff eq "!ERROR!" )
	{
		print "<font style=\"FONT-SIZE: 18pt; FONT-FAMILY: 'Calisto MT'\">";
		print "<br><br>Sorry, an error resulted decrypting.  Please try another method!";
	}
	elsif ($algorithm == 8)
	{
		#user needs to type in the key to use to decrypt
print <<TEXTBLOCK17;
		<table>
			<tr>
				<td colspan="2"><font style="FONT-SIZE: 20pt; FONT-FAMILY: 'Calisto MT'">
				<b>You chose the Find Relative Shifts between Key Letters Algorithm.<br>
				Here is a list of probable keys used for encryption:</b>
				<br><br>
				</td>
			</tr>
			<tr>
				<td colspan="2"><font style="FONT-SIZE: 16pt; FONT-FAMILY: 'Calisto MT'">
TEXTBLOCK17
		print $buff;
		while (($n = read(KES, $buff, 70))> 0)
		{	
			print $buff;
			print "<br>";
		}
print <<TEXTBLOCK18;
				<br><br></td>
			</tr>
			<tr>
				<td>
				<form name="DecryptKey" action="CrackCode.pl?4.0" method="post">
				<font style="FONT-SIZE: 14pt; FONT-FAMILY: 'Calisto MT'">Enter the key to use for decryption:</font>
				</td>
				<td>
					<textarea name="Key" cols="120" rows="5" wrap="soft"></textarea>
				</td>
			</tr>
			<tr>
				<td></td><td align="center">
					<input type="submit" value=" Submit ">
				</td>
			</tr>
				</form>
TEXTBLOCK18
	} #endif
	else
	{
		#print keyword used and plaintext.
print <<TEXTBLOCK19;
		<table>
		<tr>
			<td><font style="FONT-SIZE: 22pt; FONT-FAMILY: 'Calisto MT'">
				<b>DECRYPTION FINISHED!</b><br><br></font>
			</td>
		</tr>
			<tr>
				<td><font style="FONT-SIZE: 16pt; FONT-FAMILY: 'Calisto MT'">
TEXTBLOCK19
		print "<b>$buff</b>";
		while (($n = read(KES, $buff, 70))> 0)
		{	
			@buffer = split /:/,$buff;
			if (scalar(@buffer) == 1)
			{
				print $buff;
				print "<br>";
			}
			else
			{
				for($h = 0; $h < scalar(@buffer); $h++)
				{
					if ($buffer[$h] eq "PLAIN TEXT")
					{
						print "<br><br><b>$buffer[$h]</b><br>";
					}
					else
					{
						print "$buffer[$h] <br>";
					}
					
				}
			} 
		}
		print "<br><br>";
		print "</font></td></tr><tr><td><br><br></td></tr>";
		print "<tr><td>";
		print "<a href=\"CrackCode.pl\" style=\"FONT-SIZE: 20pt; FONT-FAMLY: 'Calisto MT'\" style=\"text-decoration: none\">";
		print "CLICK HERE TO TRY AGAIN </a></td></tr></table>";
	 }
	close(KES);
}

##################################################################
##################################################################
##Function: Decrypt
##Purpose: Call the C++ program to find the plain text of cipher.txt using the usr provided key
##		and display resulting screen
##Entry Conditions:  cipher.txt must have valid data
##Exit Conditions:  Display plain text, key, and link to allow user to try it again
##Globals:  None.
##Called By: Main
##################################################################
##################################################################
sub Decrypt()
{
	my $args =$_[0];
	#split key from description
	my @keydes = split /=/,$args;
	my $key = $keydes[1];
	#take out hex chars and restore ascii characters
	$key =~ s/%([0-9A-F][0-9A-F])/pack("c",hex($1))/ge;
	#get rid of spaces
	$key[1] =~ s/\+//g;
	#get rid of non-alphabetic characters
	$key =~ s/[^A-Za-z]//g;
	#call to cpp program to dencrypt code
	$out= `CrackCode.cgi 9 cipher.txt $key`;
	
	print <<TEXTBLOCK20;
		<table>
		<tr>
			<td><font style="FONT-SIZE: 22pt; FONT-FAMILY: 'Calisto MT'">
				<b>DECRYPTION FINISHED!</b><br><br></font>
			</td>
		</tr>
		<tr>
			<td><font style="FONT-SIZE: 16pt; FONT-FAMILY: 'Calisto MT'">
				<b>KEYWORD:</b><br>
TEXTBLOCK20
	print "$key <br><br><b>PLAIN TEXT:</b><br>";
	open(plain, "<output.txt")or die print "Cannot open output file for reading.\n";	
	#print plain text
	while (($n = read(plain, $buff, 70))> 0)
	{	
		print $buff;
		print "<br>";
	}
print "<br><br>";
		print "</font></td></tr><tr><td><br><br></td></tr>";
		print "<tr><td>";
		print "<a href=\"CrackCode.pl\" style=\"FONT-SIZE: 20pt; FONT-FAMLY: 'Calisto MT'\" style=\"text-decoration: none\">";
		print "CLICK HERE TO TRY AGAIN </a></td></tr></table>";
}