// AWT app structure + IBIO
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

 public class Crypto extends Frame implements ActionListener
 {
   //--- main starts the program, creates and displays window ---
   public static void main(String[] args)
   {
     Crypto app = new Crypto();               // Create the application

     app.setVisible(true);                    // Don't forget to display it

     app.addWindowListener                    // enable [x] for closing app
     new WindowAdapter()
       public void windowClosing(WindowEvent evt)
         System.exit(0)}
       }
     );
   }

   //--- Create Components - Buttons, TextFields, etc ---
   TextArea   aMessage  = new TextArea("Type a message here",6,40,
                                   TextArea.SCROLLBARS_VERTICAL_ONLY );
   Button     bEncrypt  = new Button("Encrypt");
   Button     bDecrypt  = new Button("Decrypt");
   Button     bHelp     = new Button("Help");
   Button     bQuit     = new Button("Exit Program");

   //--- Constructor - add components to Frame -------
   public Crypto()
   {
     super("Crypto");               // construct Frame, set Title
     setSize(350,200);              // set size of window
     setLayout(new FlowLayout());   // use FlowLayout manager

     add(aMessage);                 // add components to form
     add(bEncrypt);
     add(bDecrypt);
     add(bHelp);
     add(bQuit);

     bEncrypt.addActionListener(this);  // Register components
     bDecrypt.addActionListener(this);  //  to be handled by this
     bHelp.addActionListener(this);     //  actionPerformed method
     bQuit.addActionListener(this);

     if (checkPassword() == false)      // if password is wrong
     System.exit(0);}                 // terminate
   }

   //--- Respond to Buttons and other actions ---------
   public void actionPerformed(ActionEvent evt)
   {  Object source = evt.getSource();  // Which Button was clicked?

      if (source == bQuit)              // If Quit Button
      System.exit(0)}               //   then end the program
      else if (source == bEncrypt)      // else execute correct method
      encrypt()}                    //   according to which
      else if (source == bDecrypt)      //   Button was clicked
      decrypt()}
      else if (source == bHelp)
      showHelp();}
   }

   //--- method for doing encryption --------------
   // Mixes random extra letters in between message letters.
   // This doubles the length of the message.
   // e.g.
   //       "My message" --> "Mayk qmwexstskaagzeq"
   //----------------------------------------------
   public void encrypt()
   {
      String a = aMessage.getText();
      String b = "";

      for (int c=0; c < a.length(); c = c+1)
      {
         char ch = a.charAt(c);
         char r = (char)(int)(Math.random()*26 97);
         b = b + ch + r ;            // Add a random character r
      }                              //  in between real characters
      aMessage.setText(b);
   }

   //--- method for doing decryption ---------------
   // Retrieves only every second letter,
   // throwing out the extra random letters from the encryption.
   // e.g.
   //      "Mayk qmwexstskaagzeq" --> "My message"
   //-----------------------------------------------
   public void decrypt()
   {
      String a = aMessage.getText();
      String b = "";

      for (int c=0; c < a.length(); c = c+2)  // Take only every
      {                                       //  second char
         char ch = a.charAt(c);
         b = b + ch  ;
      }
      aMessage.setText(b);
   }

   //--- function to check for correct password ---
   // Inputs id number and password from user
   // Returns true if correct
   // Returns false if incorrect
   // Password check is not case-sensitive
   //----------------------------------------------
   public boolean checkPassword()
   {
       int id = inputInt("ID Number");
       String pw = input("Passsword");
       if ( (id == 247&& (pw.equalsIgnoreCase("ibiscool") ) )
       return true;  }
       else
       output("ID check failed");
         return false;
       }
   }

   //--- method to display help --------------------
   // Pops-up an output dialog with a few sentences.
   //-----------------------------------------------
   public void showHelp()
   {
       output("Type a message into the big text box\n" +
              "then press the [encrypt] button. \n"     +
              "Then press [decrypt] to see the original message.\n" +
              "You can double encrypt the message by\n"  +
              "pressing [encrypt] twice.\n" +
              "If you press [decrypt] before using [encrypt]\n" +
              "you will lose half the letters (non-reversible)."
             );
   }

 //===========================================================
 //=== IBIO simplified input/output commands - GUI version ===
 //===========================================================
   public void output(String message)
   {  JOptionPane.showMessageDialog(this,message);  }

   public void outputString(String message)
   {  output(message);  }

   public void output(char info)
   {  output(info + "")}

   public void output(byte info)
   {  output(info + "")}

   public void output(int info)
   {  output(info + "")}

   public void output(long info)
   {  output(info + "")}

   public void output(double info)
   {  output(info + "")}

   public void output(boolean info)
   {  output(info + "")}

   //----- Numerical input methods return 0 on error ------------
   public String input(String prompt)
   {  return JOptionPane.showInputDialog(this,prompt);  }

   public String inputString(String prompt)
   return input(prompt);  }

   public String input()
   return input("");     }

   public char inputChar(String prompt)
   char result=(char)0;
     try{result=input(prompt).charAt(0);}
     catch (Exception e){result = (char)0;}
     return result;
   }

   public byte inputByte(String prompt)
   byte result=0;
     try{result=Byte.valueOf(input(prompt).trim()).byteValue();}
     catch (Exception e){result = 0;}
     return result;
   }

   public int inputInt(String prompt)
   {  int result=0;
      try{result=Integer.valueOf(
                        input(prompt).trim()).intValue();}
      catch (Exception e){result = 0;}
      return result;
   }

   public long inputLong(String prompt)
   {  long result=0;
      try{result=Long.valueOf(input(prompt).trim()).longValue();}
      catch (Exception e){result = 0;}
      return result;
   }

   public double inputDouble(String prompt)
   {  double result=0;
      try{result=Double.valueOf(
                          input(prompt).trim()).doubleValue();}
      catch (Exception e){result = 0;}
      return result;
   }

   public boolean inputBoolean(String prompt)
   {  boolean result=false;
      try{result=Boolean.valueOf(
                         input(prompt).trim()).booleanValue();}
      catch (Exception e){result = false;}
      return result;
   }
 //============================================================
 //=========== end IBIO =======================================
 //============================================================

 }