import javax.swing.JOptionPane;
import java.util.Random;

public class  VigenereAutoKeyCipher {
    
    public  VigenereAutoKeyCipher() {
        String keyString = "Miami Heat";
        char[] keyArray = keyString.toCharArray();
        //String alphabet =  "abcdefghijklmnopqrstuvwxyz{";
        //char[] keyArray = alphabet.toCharArray();
        //permute(keyArray);
        int low = (int)'a' - 1;
        String keys = "";
        int[] key = new int[keyArray.length];
        for(int i = 0; i < key.length; i++ ) {
            key[i] = keyArray[i] - low;
            keys += key[i] + " ";
        } // end for
        keys += "\n";
        JOptionPane.showMessageDialog(null,"Combination is " + keys);
        while( true ) {
            String input = JOptionPane.showInputDialog("Enter Message","meet vaslik at the usual place at ten pm and then we will eliminate the traitor");
            if( input == null ) break;
            input = input.toLowerCase();
            String encrypted = VigenereAutoKeyEncrypt(input,key);
            JOptionPane.showMessageDialog(null,"Encrypted message is <" + encrypted + ">");
            String decrypted = VigenereAutoKeyDecrypt(encrypted,key);
            JOptionPane.showMessageDialog(null,"Decrypted message is <" + decrypted + ">");
        } // end while
    } // end constructor
    
    String VigenereAutoKeyEncrypt(String input, int[] key) {
        input = input.replace(' ','{');
        char[] message = input.toCharArray();
        String encrypted = "";
        int low = (int)'a' - 1;
        for( int i = 0; i < message.length; i++ ) {
            int charInt = (int)message[i];
            if( i < key.length ) {
                charInt = (charInt - 'a' + key[i])%27 + 'a';
            } else {
                int autokey = encrypted.charAt(i-key.length)-low;
                charInt = (charInt - 'a' + autokey)%27  + 'a';
            } // end if
            encrypted += (char)charInt;
        } // end for
        encrypted = encrypted.replace('{',' ');
        return encrypted;
    } // end VigenereAutoKeyEncrypt
    
    String VigenereAutoKeyDecrypt(String encrypted, int[] key) {
        encrypted = encrypted.replace(' ','{');
        char[] message = encrypted.toCharArray();
        String decrypted = "";
        int low = (int)'a' - 1;
        for( int i = 0; i < message.length; i++ ) {
            int charInt = (int)message[i];
            if( i < key.length ) {
                charInt = (charInt - 'a' - key[i]+27)%27 + 'a';
            } else {
                int autokey = encrypted.charAt(i-key.length)-low;
                charInt = (charInt - 'a' - autokey + 27)%27 + 'a';
            } // end if
            decrypted += (char)charInt;
        }// end for
        decrypted = decrypted.replace('{',' ');
        return decrypted;
    } // end VigenereAutoKeyDecrypt
    
    public static void main(String[] a) {
        new  VigenereAutoKeyCipher();
    } // end main
    
    public final void permute(char[] chars) {
        Random r = new Random();
        for( int i = 1; i < chars.length; i++ ) {
            char ch = chars[i];
            int j = r.nextInt(i);
            chars[i] = chars[j];
            chars[j] = ch;
        } // end for
    } // end permute
    
} // end VigenereAutoKeyCipher
