Monoalphabetic Cipher in Java With GUI An easy, terribly written but working, Caeasar (Monoalphabetic) cipher implementation, with a GUI (Graphical User Interface).

Cipher.java

class Cipher {

    static final String alphabet = "abcdefghijklmnopqrstuvwxyz";

    public static char shift(char ch, int amount) {
        return alphabet.charAt((alphabet.indexOf(ch) + amount) % alphabet.length());
    }

    public static String caesarCipher(String text, int amount) {
        String result = "";
        for (char ch : text.toCharArray()) {
            if (alphabet.indexOf(ch) &gt;= 0) {
                result += shift(ch, amount);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        assert shift('a', 1) == 'b';
        assert shift('z', 2) == 'b';
        assert caesarCipher("abc", 2).equals("cde");
    }
}


Gui.java

import javax.swing.*;        
import java.awt.*;
import java.awt.event.*;

public class Gui {

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel body = new JPanel();

        final JTextField label = new JTextField("aaaaaaaaaaaaaaaaaaaaaaa");
        body.add(label);

        final JTextField key = new JTextField("1");
        body.add(key);

        final JTextField result = new JTextField("bbbbbbbbbbbbbbbbbbbbbb");


        JButton activationButton = new JButton("Encrypt");
        activationButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                String plainText = label.getText();
                String keyval = key.getText();
                result.setText(Cipher.caesarCipher(plainText, Integer.parseInt(keyval)));
            }
        });

        body.add(activationButton);

        body.add(result);

        frame.add(body);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Input validation

It would be good to validate user input. 
If I enter "hello" as the key value, 
The UI won't like it very much.

Shifting

An idea to simplify the shifting.
Instead of taking modulo for each character,
double the alphabet (concatenate the original alphabet twice), and normalize the key value.
The solution will use twice the space for the alphabet,
but the shifting will be much faster without the modulo.

Skipping unknown characters

The current algorithm checks if a character is in the alphabet, skipping without translation if it's not. So if you convert an English sentence with space characters, the word boundaries will be lost, which will be a problem when reversing the process.

Finding the index twice

The current algorithm checks is in the alphabet, if yes it calls another to perform the shift. That method finds the index again for the second time, to calculate the shifted index. This second lookup could be eliminated if you rename "shift" to "translate" and change its behavior to lookup the index once and reuse it.

Dummy data

Instead of hardcoding the dummy result it would be better to compute it using the cipher.

Naming constants

The convention is to use SHOUTCASE for constants like the alphabet.

Testing

A bunch of assertions in a main method are no substitute for proper unit testing.