import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Vigenere extends Applet implements ActionListener {

  TextArea mText, kText, cText;
  Choice edChoice;
  Button go;

  public void init() {
    setLayout(null);
    mText = new TextArea("Enter message here for encrypting", 8, 50, TextArea.SCROLLBARS_BOTH);
    kText = new TextArea("Enter key here (in ASCII or binary, 0 is the identity key)", 2, 50, TextArea.SCROLLBARS_BOTH);
    cText = new TextArea(null, 8, 50, TextArea.SCROLLBARS_BOTH);
    Label kLabel = new Label("Key:");
    Label mLabel = new Label("Message:");
    Label cLabel = new Label("Code in ASCII:");
    go = new Button("Go!");
    edChoice = new Choice();
    
    edChoice.add("Encrypt");
    edChoice.add("Decrypt");
    edChoice.select("Encrypt");
    
    edChoice.setBounds(0, 25, 70, 25);
    go.setBounds(100, 25, 70, 25);
    mLabel.setBounds(0, 75, 100, 25);
    mText.setBounds(0, 100, 450, 125);
    kLabel.setBounds(0, 250, 100, 25);
    kText.setBounds(0, 275, 450, 75);
    cLabel.setBounds(0, 375, 100, 25);
    cText.setBounds(0, 400, 450, 125);

    add(mText);
    add(kText);
    add(cText);
    add(mLabel);
    add(kLabel);
    add(cLabel);
    add(go);
    add(edChoice);
  }
  
  public void start() {
    go.addActionListener(this);
  }
  
  public void stop() {
    go.addActionListener(null);
  }
  
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == go) {
      if (edChoice.getSelectedItem() == "Encrypt") {
	cText.setText("");
      } else {
	mText.setText("");
      }
      doIt();
    }
  }

  public void doIt() {
    char c[] = new char[1];
    String input, key = kText.getText().toUpperCase();
    TextArea output;
    int shift;
    if (edChoice.getSelectedItem() == "Encrypt") {
      input = mText.getText();
      output = cText;
    } else {
      input = cText.getText();
      output = mText;
    }
    for (int x = 0; x < input.length(); x++) {
      if (edChoice.getSelectedItem() == "Encrypt") {
	shift = key.charAt(x%key.length())%65;
      } else {
	shift = 26-(key.charAt(x%key.length())%65);
      }
      if ( (int) input.charAt(x) < 91 && (int) input.charAt(x) > 64) {
	c[0] = (char) (65+((input.charAt(x) + shift)%65)%26);
      } else if (input.charAt(x) < 123 && input.charAt(x) > 96) {
	c[0] = (char) (97+((input.charAt(x) + shift)%97)%26);
      } else {
	c[0] = input.charAt(x);
      }
      output.append(new String(c));
    }
  }

  /*
  public String encode(String code, String key) {
    String message = new String();
    if (asciiChoice.getSelectedItem() == "Forced ASCII") {
      if (edChoice.getSelectedItem() == "Decrypt") {
	code = strip(code);
      } else { //Forced ASCII encoding - padding
	for (int x = 0; x < code.length(); x++) {
	  if (x % 5 == 0) {
	    message += "010";
	  }
	  message += XOR(code.charAt(x), key.charAt(x % key.length()));
	}
	while (message.length() % 8 != 0) {
	  message += "0";
	}
	return message;
      }
    }
    for (int x = 0; x < code.length(); x++) {
      message += XOR(code.charAt(x), key.charAt(x % key.length()));
    }
    return message;
  }
  */

}
