/*
 * ToDo:
 *
 *
 */

   import java.awt.*;
   import java.applet.*;
   import java.awt.event.*;
   import java.net.URL;
   import java.io.*;
   import java.util.zip.*;

/**
 * BIAESFrEnd - Tim Tyler 2001.
 * 
 * A front end for BIAES.<P>
 * This code has been placed in the public domain.<P>
 * You can do what you like with it.<P>
 * Note that this code comes with no warranty.<P>
 *
 */
   public class BIAESFrEnd extends java.applet.Applet implements ActionListener {
      static String document_base;
      static String java_version;
      static String directory_separator;
      static URL base_url;
   
      static Applet applet;
   
      static Panel input, output, input1, input2, input3;
      static Panel output1, output2, output3, output4, output5;
   
      static Frame frame;
   
      static boolean application = false;
   
      final static boolean development_version = false;
      // final static boolean compressed = true;
   
      static int i=0, j=0, k=0, n=0;
   
      static Checkbox checkbox_iv;
   
      static TextField original_file_textfield;
      static TextField encrypted_file_textfield;
      static TextField decrypted_file_textfield;
   
      static TextField key_textfield;
   
      static Button original_file_button;
      static Button encrypted_file_button;
      static Button decrypted_file_button;
   
      static Button encrypt_button;
      static Button decrypt_button;
   
      static BIAES biaes = new BIAES();
   
   // constructor
      public BIAESFrEnd() {
         setLayout(new GridLayout(5,1));
         Font std_font = new Font("Helevetica", Font.BOLD, 16);
      
         output1 = new Panel();
         output1.setBackground(Color.lightGray);
         output2 = new Panel();
         output2.setBackground(Color.lightGray);
         output3 = new Panel();
         output3.setBackground(Color.lightGray);
         output4 = new Panel();
         output4.setBackground(new Color(0xCFCFCF));
         output5 = new Panel();
         output5.setBackground(Color.lightGray);
      
         Label label_temp;
      
         Panel panel_original_file = new Panel();
         label_temp = new Label("   Original file:", Label.RIGHT);
         label_temp.setFont(std_font);
         panel_original_file.add(label_temp);
         original_file_textfield = new TextField("",66);
         original_file_textfield.setFont(std_font);
         panel_original_file.add(original_file_textfield);
         original_file_button = new Button("Browse");
         original_file_button.setFont(std_font);
         original_file_button.addActionListener(this);
         panel_original_file.add(original_file_button);
         output1.add(panel_original_file);
      
         Panel panel_encrypted_file = new Panel();
         label_temp = new Label("Encrypted file:", Label.RIGHT);
         label_temp.setFont(std_font);
         panel_encrypted_file.add(label_temp);
         encrypted_file_textfield = new TextField("",66);
         encrypted_file_textfield.setFont(std_font);
         panel_encrypted_file.add(encrypted_file_textfield);
         encrypted_file_button = new Button("Browse");
         encrypted_file_button.setFont(std_font);
         encrypted_file_button.addActionListener(this);
         panel_encrypted_file.add(encrypted_file_button);
         output2.add(panel_encrypted_file);
      
         Panel panel_decrypted_file = new Panel();
         label_temp = new Label("Decrypted file:", Label.RIGHT);
         label_temp.setFont(std_font);
         panel_decrypted_file.add(label_temp);
         decrypted_file_textfield = new TextField("",66);
         decrypted_file_textfield.setFont(std_font);
         panel_decrypted_file.add(decrypted_file_textfield);
         decrypted_file_button = new Button("Browse");
         decrypted_file_button.setFont(std_font);
         decrypted_file_button.addActionListener(this);
         panel_decrypted_file.add(decrypted_file_button);
         output3.add(panel_decrypted_file);
      
         Panel panel_key = new Panel();
         label_temp = new Label("Key phrase:", Label.RIGHT);
         label_temp.setFont(std_font);
         panel_key.add(label_temp);
         key_textfield = new TextField("Don't use this key phrase",55);
         key_textfield.setFont(std_font);
         panel_key.add(key_textfield);
         output4.add(panel_key);
      
         checkbox_iv = new Checkbox("Use an initial value");
         checkbox_iv.setFont(std_font);
         output4.add(checkbox_iv);
      
         output5.setLayout(new GridLayout(1,2));
      
         encrypt_button = new Button("Encrypt");
         encrypt_button.setFont(std_font);
         encrypt_button.addActionListener(this);
         output5.add(encrypt_button);
      
         decrypt_button = new Button("Decrypt");
         decrypt_button.setFont(std_font);
         decrypt_button.addActionListener(this);
         output5.add(decrypt_button);
      
         add(output1);
         add(output2);
         add(output3);
         add(output4);
         add(output5);
      }
   
   
      void enableButtons(boolean flag) {
         encrypt_button.setEnabled(flag);
         decrypt_button.setEnabled(flag);
      }
   
   
      public void actionPerformed(ActionEvent ev) {
         if (ev.getSource() == encrypt_button) {
            enableButtons(false);
            try {
               encrypt();
            }
               catch (RuntimeException e) {
                  Log.log("Error while encrypting:");
                  e.printStackTrace(Log.getPrintStream());
               }
         
            enableButtons(true);
         }
      
         if (ev.getSource() == decrypt_button) {
            enableButtons(false);
            try {
               decrypt();
            }
               catch (RuntimeException e) {
                  Log.log("Error while decrypting:");
                  e.printStackTrace(Log.getPrintStream());
               }
         
            enableButtons(true);
         }
      
         if (ev.getSource() == encrypted_file_button) {
            browseEncryptedFile();
         }
      
         if (ev.getSource() == decrypted_file_button) {
            browseDecryptedFile();
         }
      
         if (ev.getSource() == original_file_button) {
            browseOriginalFile();
         }
      }
   
   
      void getDocBase() {
         if (document_base == null) {
         
            // get Java version...
            java_version = System.getProperty("java.version");
            directory_separator = System.getProperty("file.separator");
         
            if (application) {
               try {
                  document_base = System.getProperty("user.dir");
                  base_url = new URL("file:/" + document_base + directory_separator);
               }
                  catch (Exception e2) {
                     Log.log("Error getting application base: " + e2.toString());
                  }
            }
            else
            {
               try {
                  // base_url = getDocumentBase(); // applet
                  base_url = getCodeBase(); // applet
                  document_base = base_url.toString();
                  String base_url_s = base_url.toString(); // applet
               
                  // trailing "."...?
                  if (base_url_s.indexOf("/.") > 0) {
                     Log.log("Warning: slash-fullstop in URL - truncating URL to avoid bug.");
                     document_base = document_base.substring(0, document_base.length() - 2);
                     base_url = new URL(base_url_s.substring(0, base_url_s.length() - 1));
                  }
               }
                  catch (Exception e) {
                     Log.log("Error getting applet base: " + e.toString());
                  }
            }
         }
      }
   
   
   
      static void encrypt() {
      
         String original_file = original_file_textfield.getText();
         String encrypted_file = encrypted_file_textfield.getText();
         String key_string = key_textfield.getText();
         boolean iv = checkbox_iv.getState();
         biaes.encrypt(original_file, encrypted_file, key_string, iv);
      
      }
   
   
      static void decrypt() {
         String encrypted_file = encrypted_file_textfield.getText();
         String decrypted_file = decrypted_file_textfield.getText();
         String key_string = key_textfield.getText();
         boolean iv = checkbox_iv.getState();
         biaes.decrypt(encrypted_file, decrypted_file, key_string, iv);
      }
   
      static void browseOriginalFile() {
         FileDialog fd = new FileDialog(frame, "Select file", FileDialog.LOAD);
      
         fd.setFile("input.dat");
      
         fd.show();
         String returnedstring = fd.getFile();
         if (returnedstring != null) {
            if (!returnedstring.equals("")) {
               original_file_textfield.setText(fd.getDirectory() + returnedstring);
            }
         }
      }
   
   
      static void browseEncryptedFile() {
         FileDialog fd = new FileDialog(frame, "Select file", FileDialog.LOAD);
      
         fd.setFile("input.dat");
      
         fd.show();
         String returnedstring = fd.getFile();
         if (returnedstring != null) {
            if (!returnedstring.equals("")) {
               encrypted_file_textfield.setText(fd.getDirectory() + returnedstring);
            }
         }
      }
   
      static void browseDecryptedFile() {
         FileDialog fd = new FileDialog(frame, "Select file", FileDialog.LOAD);
      
         fd.setFile("output.dat");
      
         fd.show();
         String returnedstring = fd.getFile();
         if (returnedstring != null) {
            if (!returnedstring.equals("")) {
               decrypted_file_textfield.setText(fd.getDirectory() + returnedstring);
            }
         }
      }
   
      public static void main(String args[]) {
         application = true;
      
         BIAESFrEnd applet = new BIAESFrEnd();
      
         frame = new TTAppletFrame("BIAES",(Applet)applet,850,288);
         Image image = frame.createImage(29,29);
         Graphics g = image.getGraphics();
         g.setColor(Color.black);
         g.fillRect(0,0,30,30);
         g.setColor(Color.yellow);
         g.drawRect(0,0,28,28);
      
         for (int i = 1; i < 14; i++) {
            int temp = i * 14;
            g.setColor(new Color((temp << 8) | (temp ^ 0xFF)));
         
         
            g.drawRect(i,i,28 - i - i,28 - i - i);
         }
      
         frame.setIconImage(image);
      }
   
   }