package KeyDistribute;

import java.lang.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


//*========================================================================*
//*    WELCOME TO << CERT AND EXODUS KEY MANAGEMENT SYSTEM >>              *
//*                                                                        *
//*    This programs and methodology is propected by                       *
//*    U.S. and International Copy Right                                   *
//*                                                                        *
//*    Any attempt to copy or replicate all or any part of this            *
//*    property is subject to full extension of legal                      *
//*    prosecution                                                         *
//*------------------------------------------------------------------------*
//*    This program was designed and developed by Hitae Lee                *
//*------------------------------------------------------------------------*

public class KeyDistribute extends HttpServlet {

   public void doGet(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException, IOException
    {
       int i, j;
       String outKey;

       res.setContentType("text/html");
       PrintWriter out = res.getWriter();

       // E-Commerce will be charged under this account
       // We will add an additional SQL logic to create
       // a billing record -- billing would be free though

       String account = req.getParameter("Account");

       out.println("<HTML>");
       out.println("<HEAD><TITLE> Message from RSA public key distribution Management System </TITLE></HEAD>");
       out.println("<BODY>");
       out.println("<body bgcolor=\"#FFFFFF\" >");
       out.println("<p><font face=\"Arial\"><small>");
       out.println(" Congratulation!!! <br><br><br> ");
       out.println(" You made very smart decision to protect your important financial <br> ");
       out.println(" information from would be attackers by encrypting it using <br> ");
       out.println(" patented method of RSA and CertandExodus 3-way encryption/decryption <br><br><br> ");
       out.println(" Please wait we are creating digital envelop using RSA's public/private key,<br> ");
       out.println(" your password and CertandExodus bit manipulator...<br> <br>");

       String password = req.getParameter("Password");
       // E-Commerce may define this field as Hidden to optimize their
       // service fee to CertandExodus Venture Inc.
       String   skeyLength = req.getParameter("KeyLength");
       int keyLength = Integer.parseInt(skeyLength, 10);
       KeyDatabase oKeyDB;
       oKeyDB = new KeyDatabase();
       oKeyDB.Connect();
       int keyLengthType = 0;
       if(keyLength == 512)
         {
           keyLengthType = 1;
         }
       if(keyLength == 768)
         {
           keyLengthType = 2;
         }
       if(keyLength == 1024)
         {
           keyLengthType = 3;
         }
       if(keyLength == 1280)
         {
           keyLengthType = 4;
         }
       if(keyLength == 1536)
         {
           keyLengthType = 5;
         }
       if(keyLengthType == 0)
         {
         out.println(" Your merchant did not specify a correct key length <br> ");
         out.println(" The default key length of 768 bits will be used <br> ");
         keyLengthType = 2;
         }
       PublicKeyRec key;
       key = oKeyDB.getKey(keyLengthType);
       byte[] PublicKey = null;
       PublicKey = oKeyDB.getPublicKey(key);
       RandomRec rkey;
       rkey = oKeyDB.getRandomKey(keyLength, key.getkeySequence());
       byte[] Random = null;
       Random = oKeyDB.getRandom(rkey);
       out.println(" Your key has been selected from our repository database... <br> ");
       oKeyDB.markDistribution(keyLength, key.getkeySequence());
       oKeyDB.close();

       // Now we need encrypt the public and creates digital envelop
       pbePublicKey oPbEncryptor;
       oPbEncryptor = new pbePublicKey();
       byte[] Certificate = null;
       Certificate = oPbEncryptor.pbEncrypt(password.toUpperCase(), PublicKey, keyLength, key.getkeySequence());
       out.println(" Key distribution has been completed successfully <br><br><br> ");
       out.println(" The same key and your password will be used anytime  <br> ");
       out.println(" you submit your credit card to merchant who uses our service <br> ");
       out.println(" Please inquire your merchant by clicking <a href=\"http://www.certificateissuer.com\">here</a> ...  <br> <br> <br> ");
       out.println(" If you want to use the same key and password to shop anywhere in world<br> ");
       out.println(" Please inquire <a href=\"http://www.chipcardandCA.com\">here</a> to make a smartcard with your personal information<br> ");
       byte[] fullCertificate = null;
       Integer i1 = new Integer(Random.length);
       Byte b1 = new Byte(i1.byteValue());
       fullCertificate[0] = b1.byteValue();
       for(i = 0; i <Random.length; i++)
       {
          fullCertificate[i + 1] = Random[i];
       }
       for(i = 0; i <7; i++)
       {
          j = Random.length;
          Integer i2 = new Integer(187);
          Byte b2 = new Byte(i2.byteValue());
          fullCertificate[i + 1 + j] = b2.byteValue();
       }
       for(i = 0; i <Certificate.length; i++)
       {
          j = Random.length;
          fullCertificate[i + 7 + j] = Certificate[i];
       }

       outKey = getString(fullCertificate);
       out.println(" along with COOL smartcard reader which can be easily attached to your PC <br><br><br> ");
       out.println(outKey + " <br><br><br> </p> ");
       out.println("</small><br>");
       out.println("</body>");
       out.println("</html>");

      }
   // keyLength must be 64 for 512, 96 for 768, 128 for 1024 and 256 for 2048
   public String getString (byte[] inKey)
      {
         String strKey;
         int i1, i2;

         strKey = "";
         for(int i = 0; i < inKey.length; i++)
         {
            i1 = inKey[i] % 16;
            i2 = inKey[i] / 16;
            if(i2 * 16 > inKey[i])
              {
                i2--;
              }
            if(i2 == 15) strKey = strKey + "F";
            if(i2 == 14) strKey = strKey + "E";
            if(i2 == 13) strKey = strKey + "D";
            if(i2 == 12) strKey = strKey + "C";
            if(i2 == 11) strKey = strKey + "B";
            if(i2 == 10) strKey = strKey + "A";
            if(i2 == 9) strKey = strKey + "9";
            if(i2 == 8) strKey = strKey + "8";
            if(i2 == 7) strKey = strKey + "7";
            if(i2 == 6) strKey = strKey + "6";
            if(i2 == 5) strKey = strKey + "5";
            if(i2 == 4) strKey = strKey + "4";
            if(i2 == 3) strKey = strKey + "3";
            if(i2 == 2) strKey = strKey + "2";
            if(i2 == 1) strKey = strKey + "1";
            if(i2 == 0) strKey = strKey + "0";

            if(i1 == 15) strKey = strKey + "F";
            if(i1 == 14) strKey = strKey + "E";
            if(i1 == 13) strKey = strKey + "D";
            if(i1 == 12) strKey = strKey + "C";
            if(i1 == 11) strKey = strKey + "B";
            if(i1 == 10) strKey = strKey + "A";
            if(i1 == 9) strKey = strKey + "9";
            if(i1 == 8) strKey = strKey + "8";
            if(i1 == 7) strKey = strKey + "7";
            if(i1 == 6) strKey = strKey + "6";
            if(i1 == 5) strKey = strKey + "5";
            if(i1 == 4) strKey = strKey + "4";
            if(i1 == 3) strKey = strKey + "3";
            if(i1 == 2) strKey = strKey + "2";
            if(i1 == 1) strKey = strKey + "1";
            if(i1 == 0) strKey = strKey + "0";

         }

         return strKey;
      }

   public void doPost(HttpServletRequest req, HttpServletResponse res)
   {
       try
         {
           doGet(req, res);
        }
      catch (ServletException e)
         {
           System.out.println("Servlet is not supported");
         }
      catch (IOException e)
         {
           System.out.println("General IO Error");
         }

    }


}











