// (adapted) from http://novocode.de/doc/servlet-essentials/chapter2b.html



import java.util.Vector;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class serv extends HttpServlet
{
  //private Vector addresses;
  //private String filename;
   String temp = "";
	
   String but="";
	String plain ="";
	String key="";
	String cipher="";
   int blockSize = 0;
	int keySize =0;
	String explanation = "<b><font color = red>Rijndael</font></b><br>To Encrypt a message using Rijndael, type message into Plaintext, " +
	"type in a Key and select block size and key size (or leave as default). Click Encrypt Rijndael.<br> " +
	"To Decrypt a cipher using Rijndael, type the ciphertext into Cipher, type in a Key and press Decrypt Rijndael. " +
	"<p><b><font color = red>El Gamal</font></b><br>To Encrypt a message for me, type in a message and leave the key blank (it will use my public " +
	"key as default).<br> To Encrypt a message for somebody else, type a message into Plaintext and enter their Key. " +
	"The public key must be of the format \"(p,g,y)\". My p,g,y values are below as an example. <a href=\"http://homepage.eircom.net/~cass/security.html\">For further information about this work, follow this link.</a><br> " + 
	"It is only possible to decrypt a message for me. Type in the cipher and click Decrypt El Gamal. My private key is " +
	"just that. Private.";

  public void init(ServletConfig config) throws ServletException  {
    super.init(config);
  }
  protected void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException ,IOException{
    res.setContentType("text/html");
    res.setHeader("pragma", "no-cache");
    PrintWriter out = res.getWriter();
	// myPrint(out);
	but="";
	plain ="";
	key="";
	cipher="";
	temp = "doGet";
	myPrint(out);
	

  }
 
public void myPrint(PrintWriter out){
	 out.print("<html><head><title>Encryption Servlet</title></head>");

out.print("<body text = blue><form METHOD=POST>");
out.print("<center><h1><font color = red>Security Page</font></h2></center>");
out.print("<h2>Instructions:</h2>");
//out.print("<h3>To Follow....</h3>");
out.print(explanation);
out.print("<br><br><font color = navy><b>My public key is as follows (for El Gamal):</b></font>");
out.print("<br>P: 14893003337626352152463254152616458181260144281");
out.print("<br>G: 4893003337626352152463254152616458181260144281");
out.print("<br>Y: 5260810279682188795512623296546807031696158558");
out.print("<table>");
out.print("   <tr>");
out.print("      <td><font color = navy>Plain:</font></td>");
out.print("     <td><textarea name=plain rows='8' cols='80'>");
out.print(plain);
out.print("</textarea></td>");
out.print("   </tr>");
out.print("   <tr>");
out.print("      <td><font color = navy>Key:</font></td>");
out.print("      <td><textarea name=key row ='4' cols='80'>");
out.print(key);
out.print("</textarea></td>");
out.print("   </tr>");
out.print("   <tr>");
out.print("      <td><font color = navy>Cipher:</font></td>");
out.print("      <td><textarea name=cipher rows='8' cols='80'>");
out.print(cipher);
out.print("</textarea></td>");
out.print("   </tr>");
out.print("</table>");
out.print("<table>");
out.print("   <tr>");
out.print("      <td><b><font color = navy>Rijndael Parameters</font></td></b>");
out.print("   </tr>");
out.print("   <tr>");
out.print("      <td>Block size:</td>");
out.print("   </tr>");
out.print("   <tr>");
out.print("      <td><select name='blocksize'>");
out.print("         <option selected value= '128'>128</option>");
out.print("         <option value= '192'>192</option>");
out.print("         <option value= '256'>256</option></select>");
out.print("      </td>");
out.print("   </tr>");
out.print("   <tr>");
out.print("      <td>Key Size:</td>");
out.print("   </tr>");
out.print("   <tr>");
out.print("      <td><select name='keysize'>");
out.print("         <option selected value= '128'>128</option>");
out.print("         <option value= '192'>192</option>");
out.print("         <option value= '256'>256</option></select>");
out.print("      </td>");
out.print("   </tr>");
out.print("   <tr>");
//out.print("<td><INPUT type= 'radio' name='algorithm' value='Rijndael' id='r'></td>");
//out.print("<td><INPUT type='radio' name='algorithm' value='El Gamal' id='e'></td>");

out.print("      <td><input type=SUBMIT name=ACTION value='Encrypt Rijndael'></td>");
out.print("      <td><input type=SUBMIT name=ACTION value='Encrypt El Gamal'></td>");
       
out.print("   </tr>");
out.print("   <tr>");

//out.print("<td><INPUT type= radio name= crypt  value= Encrypt id=e></td>");
//out.print("<td><INPUT type= radio name= crypt  value= Decrypt id=d></td>");

out.print("      <td><input type=SUBMIT name=ACTION value='Decrypt Rijndael'></td>");
out.print("      <td><input type=SUBMIT name=ACTION value='Decrypt El Gamal'></td>");

out.print("   </tr>");
out.print("</table>");	
out.print(temp);
out.print("</form></body></html>");
out.close();
  }
	
  protected void doPost(HttpServletRequest req,
			HttpServletResponse res)
            throws ServletException, IOException
  {
   
	but="";
	if(req.getParameter("ACTION").equals("Decrypt Rijndael")){
		but = "dr";
	} else if (req.getParameter("ACTION").equals("Encrypt Rijndael")){
		but = "er";
	} else if (req.getParameter("ACTION").equals("Encrypt El Gamal")){
		but = "ee" ;
	} else if (req.getParameter("ACTION").equals("Decrypt El Gamal")){
		but ="de";
	}
	plain = req.getParameter("plain");
	key = req.getParameter("key");
	cipher = req.getParameter("cipher");
	blockSize = new Integer(req.getParameter("blocksize")).intValue();
	keySize = new Integer (req.getParameter("keysize")).intValue();
	if(but == "er"){
		rijnMaker r1 = new rijnMaker(blockSize/8,keySize/8);
		cipher = r1.encrypt(plain,key);
	}else if (but == "dr"){
		rijnMaker r2 = new rijnMaker(blockSize/8,keySize/8);
		plain = r2.decrypt(cipher,key);
	}else if(but == "ee"){
		elGamal e1 = new elGamal();
		if(key.length() == 0){
			cipher = e1.bigEncrypt(plain);
		}else {
			cipher = e1.bigEncrypt(plain,key);
		}
	}else if(but == "de"){
		elGamal e2 = new elGamal();
		plain = e2.bigDecrypt(cipher);
	}
	
	res.setContentType("text/html");
   res.setHeader("pragma", "no-cache");
   PrintWriter out = res.getWriter();
	//temp = "doPost" + but + " " + but.length() + "key: " + key + "\n" + cipher;
	if(but.length()<2){
		but="";
		plain ="";
		key="";
		cipher="";
		blockSize = 0;
		keySize = 0;
	}
	myPrint(out);
	
	}

	public String getServletInfo() {
		return "SecurityServlet 1.0 by Cass Crockatt";
	}
 
}
