import java.applet.Applet;
import java.awt.*;
import java.util.Random;
import Crypt;

/**
 * UNIX Passwort crypter.
 * This applet encrypts an entered plaintext password with
 * the unix <CODE>crypt()</CODE>-function .
 *
 * @author Philipp Hahn
 * @version 1 14 Aug 1998
 */
public class JavaCrypt extends Applet
{
  protected TextField text = null;
  protected TextField passwd = null;
  protected Crypt crypt = new Crypt ();
  protected Random rnd = new Random ();

  private GridBagLayout gridbag = null;
  private GridBagConstraints c = null;

  /**
   * Salt is a two-character string chosen from the set [a-zA-Z0-0./].
   */
  public static final String SALT =
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";

  /**
   * Get one random salt-character.
   */
  private char getSalt ()
  {
    int r = rnd.nextInt ();
    if (r < 0) r = -r;
    r %= SALT.length ();
    return SALT.charAt (r);
  }

  /**
   * Add component add gridlocation x|y.
   * @PARAM comp Component to add
   * @PARAN x Grid-location in colum
   * @PARAM y Grid-location in row
   */
  private void add (Component comp, int x, int y)
  {
    c.gridx = x;
    c.gridy = y;
    gridbag.setConstraints (comp, c);
    add (comp);
  }

  /**
   * Create all visual components.
   */
  public JavaCrypt ()
  {
    text = new TextField ("",8);
    text.setEchoCharacter ('*');

    passwd = new TextField ("",14);
    passwd.setEditable (false);

    gridbag = new GridBagLayout ();
    setLayout (gridbag);
    c = new GridBagConstraints ();
    c.fill = GridBagConstraints.BOTH; c.ipadx = 5;

    c.gridwidth = GridBagConstraints.RELATIVE; c.weightx = 0.0;
    add (new Label ("Plaintext:"), 0, 0);
    c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0;
    add (new Label ("Encrypted:"), 1, 0);

    c.anchor = GridBagConstraints.WEST;

    c.gridwidth = GridBagConstraints.RELATIVE; c.weightx = 0.0;
    add (text, 0, 1);
    c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0;
    add (passwd, 1, 1);

    text.requestFocus ();
  }

  /** 
   * Some information about this applet.
   */
  public String getAppletInfo ()
  { return "Philipp Hahn, Version 1"; }

  /**
   * Handle events for all components.
   */
  public boolean action (Event evt, Object arg)
  {
    if (evt.target == text)
    {
      String plain = (String)arg;
      char c[] = new char[2];
      c[0] = getSalt ();
      c[1] = getSalt ();
      String salt = new String (c);
      String code = this.crypt.decode (plain, salt);
      passwd.setText (code);
    }
    return true;
  }

  /**
   * Main routine.
   * Runs this applet as an application
   */
  public static void main (String args[])
  {
    Frame f = new Frame("JavaCrypt");
    try {
      f.addWindowListener(
        new java.awt.event.WindowAdapter() {
          public void windowClosing(java.awt.event.WindowEvent e)
          { System.exit(0); }
        }
      );
    } catch(Exception e)
    { ; }
    JavaCrypt javaCrypt = new JavaCrypt ();
    javaCrypt.init();
    javaCrypt.start();
    f.add ("Center", javaCrypt);
    f.resize (250, 100);
    f.show ();
  }
}

