/*
* Crypto.java
*
* Created on 20. Mai 2006, 17:41
*
*    [This programm shows some cryptograhps things]
*    Copyright (C) [2006]  [Daniel Baier alias duddits]
*
*    This program is free software; you can redistribute it and/or modify it under the terms of the
*    GNU General Public License as published by the Free Software Foundation; either version 2 of the
*    License, or (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
*    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*    See the GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along with this program;
*    if not, write to the
*    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*
*
*
*
*    [http://www.kuno-kohn.de/crypto/crypto/caesar.htm
*     Dieses Programm zeigt die Möglichkeiten beim ver-/entschlüsseln mit
*     einem monoalphabetischen Chiffre der einen Substitutionsalgorithmus verwendet]
*    Copyright (C) [2006]  [Daniel Baier alias duddits]
*
*    Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General
*    Public License, wie von der Free Software Foundation veröffentlicht, weitergeben und/oder
*    modifizieren, entweder gemäß Version 2 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
*
*    Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird,
*    aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder der
*    VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
*
*    Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten
*    haben. Falls nicht, schreiben Sie an die
*    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
*
*
*/


/**
*
* @author daniel baier
*/
import java.awt.*;
import java.awt.event.*;

public class Crypto extends Frame implements ActionListener{

private static final long serialVersionUID = 1L;
Label en,de,k;
TextField ecrypt,decrypt,key;
static Crypto g = new Crypto();

Button action,clear;
/** Creates a new instance of GUI */
public Crypto() {
    //main settings
    super("Crypto");
    setSize(500,350);
    setLayout(null);
    setBackground(Color.white);

    // field to enter the text for the encryption
    de = new Label("Enter the text you want to encrypt:");
    de.setBounds(20,20,300,40);
    add(de);

    decrypt = new TextField();
    decrypt.setBounds(20,70,300,40);
    add(decrypt);

    // button to start the encryptions
    action = new Button("encrypt");
    action.setBounds(350,70,100,40);
    action.addActionListener(this);
    add(action);

    // a button to clear the fields
    clear = new Button("clear");
    clear.setBounds(350,140,100,40);
    clear.addActionListener(this);
    add(clear);

     // button to start the decryptions
    action = new Button("decrypt");
    action.setBounds(350,210,100,40);
    action.addActionListener(this);
    add(action);

    // field to enter the key for encrypting the text
    k = new Label("Length for the transposition:");
    k.setBounds(20,120,300,40);
    add(k);

    key = new TextField();
    key.setBackground(Color.black);
    key.setForeground(Color.white);
    key.setBounds(20,160,200,40);
    add(key);

    // the result of the encryption
    en = new Label("Encryptet text:");
    en.setBounds(20,220,300,40);
    add(en);

    ecrypt = new TextField();
    ecrypt.setBounds(20,260,300,40);
    add(ecrypt);

    this.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
        	dispose();
        }
    });
}


// Methode für die Verschlüsselung mit dem String Parameter sold
public String encrypt (String sold) throws Exception
{
String s = sold.toLowerCase();
char character[] = {'z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y'};
           int ik = Integer.parseInt(key.getText());  // int Wert der die Zahlen-Wertigkeit vom Klartextbuchstaben festhält
           char c;// char Wert in dem der Klartextbuchstabe hinterlegt wird
           int ci = 0;
           StringBuffer encryptstr = new StringBuffer (s.length());
           int i = 0;
           int j = 0;

                while(j < s.length()){

               c = s.charAt(j);
               i = 0;
               if(c >= 'a' && c <= 'z'){
               for(; i < character.length ; i++){

                    if(character[i] == c){
                        ci = i;
               }

                     }
               encryptstr.insert (j, character[(ci+ik)%26]);

               }else{
            	   encryptstr.insert (j, c);
            	   }
               j++;
               }


return encryptstr.toString();
}



 // Methode für die Entschlüsselung mit dem String Parameter sold
public String decrypt (String sold) throws Exception
{
String s = sold.toLowerCase();
char character[] = {'z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y'};
           int ik = Integer.parseInt(key.getText());  // int Wert der die Zahlen-Wertigkeit vom Klartextbuchstaben festhält
           char c;// char Wert in dem der Klartextbuchstabe hinterlegt wird
           int ci = 0;
           StringBuffer decryptstr = new StringBuffer (s.length());
           int i = 0;
           int j = 0;
                while(j < s.length()){

               c = s.charAt(j);
               i = 0;
               if(c >= 'a' && c <= 'z'){
               for(; i < character.length ; i++){

                    if(character[i] == c){
                        ci = i;
               }

                     }
               decryptstr.insert (j, character[(ci-ik+26)%26]);

               }else{
            	   decryptstr.insert (j, c);
        	   }
           j++;}

return decryptstr.toString();
}

public void actionPerformed(ActionEvent ae){
    String asave = ae.getActionCommand();
    if(asave.equals("encrypt")){
        try{
        String sold = decrypt.getText();
        String snew = encrypt(sold);
        ecrypt.setText(snew);
        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }else if(asave.equals("clear")){
        ecrypt.setText("");
        decrypt.setText("");
        key.setText("");
    }else if(asave.equals("decrypt")){
        try
        {
         String sold = ecrypt.getText().toLowerCase();
          String snew = decrypt(sold);
        decrypt.setText(snew);
        }catch(Exception e){
            System.err.println(e.getMessage());
        }


    }
}

public static void main(String args[]){
    g.setVisible(true);
}

}



