First: Ceasar.java
This file is a console version that will allow you to encode/decode messages using the Ceasar code.

Second: CeasarGui.java
This file is a GUI version that calls the preceding one and allow, in a GUI environment, to encode/decode messages changing with JRadioButton the number of shifts to apply.

Ceasar.java

import java.util.Scanner;
/*
* The first registered secret code that was used by Julius Ceasar (or at least his army)
* during the Gaul war 50 BC
* All letters in the alphabet are shifted by a certain number of letters
* The number of shifts must be known by the sender and the receiver
* So if shift == 1
* abcdefghijklmnopqrstuvwxyz will be coded as
* BCDEFGHIJKLMNOPQRSTUVWXYZA
* if shift == 5
* abcdefghijklmnopqrstuvwxyz will be coded as
* FGHIJKLMNOPQRSTUVWXYZABCDE
*/
public class Ceasar {
// the number of letter(s) we shifted
private int nbShift;
/**
* constructor that receives the number of letters we will shift
* @param nbShift
*/
public Ceasar(int nbShift) {
setShiftValue(nbShift);
}
/**
* To set the initial or change the shift value
* @param nbShift
*/
public void setShiftValue(int nbShift) {
// wrap around shift > 26 and accept only positive shifts
if(nbShift < 0)
nbShift = -nbShift;
// store the number of shift to be applied
this.nbShift = nbShift % 26;
}
/**
* this method returns the encrypted String from the String received as parameter
* according to the current number of letter(s) to shift in the alphabet
*/
public String encode(String clear) {
// ignore if null
if(clear == null)
return "";
// by convention in the cryptology world all original messages are in lower case
// and coded messages in uppercase
// so let us put the original message in lower cases
// convert the original message into an array of char
// into which converted character will be put later
char[] digit = clear.toLowerCase().toCharArray();
// pass through all input digits to convert them
for(int i = 0; i < digit.length; i++) {
// if they are not between 'a' and 'z' just ignore them (message has been translated to lowercase)
if(digit[i] < 'a' || digit[i] > 'z')
continue;
// convert the digit to uppercase (as we mentionned this is a standard cryptographic convention)
digit[i] = Character.toUpperCase(digit[i]);
// applied the shift to it
digit[i] += nbShift;
// wrap around if over 'Z'
if(digit[i] > 'Z') {
digit[i] -= 'Z';
digit[i] += ('A' - 1);
}
}
// return a new String made of these shifted characters
return new String(digit);
}
/**
* this method returns the decrypted String from the String received as parameter
* according to the current number of letter(s) to shift in the Ceasar code
*/
public String decode(String coded) {
// ignore if null
if(coded == null)
return "";
// by convention in the cryptology world all original messages are in lowercode
// and coded messages in uppercase
// so let us put the original message in upper cases
// convert the original message into an array of char
char[] digit = coded.toUpperCase().toCharArray();
// pass through all input coded digits to convert them
for(int i = 0; i < digit.length; i++) {
// if they are not between 'A' and 'Z' just ignore them (message has been translated to upperrcase)
if(digit[i] < 'A' || digit[i] > 'Z') {
continue;
}
// convert the digit to lowerrcase (as we mentionned this is a standard cryptographic convention)
digit[i] = Character.toLowerCase(digit[i]);
// applied the shift to it
digit[i] -= nbShift;
// wrap around if before 'A'
if(digit[i] < 'a') {
digit[i] += 'z';
digit[i] -= ('a' - 1);
}
}
// return a new String made of these shifted characters
return new String(digit);
}
/**
* the main() method to test our Ceasar class
* @param args
*/
public static void main(String[] args) {
//--------------------------------------------------------
// Automatic tests
//--------------------------------------------------------
// to test with all possible shifts
// the following line can be removed (or commented) in production code
testAllShifts(); // <---- to be removed in production code
//--------------------------------------------------------
// End of Automatic tests
//--------------------------------------------------------
// now test a real case
// Creates a Scanner to read use input
Scanner scan = new Scanner(System.in);
// read the number of shift
// init number of shift to an integer valid value
int nbShift = 0;
// loop until we break for a valid value
while(true) {
System.out.print("Enter number of letters to shift: ");
String str = scan.nextLine();
// try to translale String inputed as Integer
try {
nbShift = Integer.parseInt(str);
// if the catch case was not invoked it means this is a valid integer input
// so we break out the loop
break;
}
catch(Exception e) {
System.out.println("Sorry your nbumber of shifts is invalid");
}
}
// now that we have a valid shift create a Ceasar object with the number of shifts
Ceasar ceasar = new Ceasar(nbShift);
// input the line to be coded
System.out.print("Enter message to be coded: ");
String clearMsg = scan.nextLine();
// code the user input
String codedString = ceasar.encode(clearMsg);
// print the coded string
System.out.println("The coded String is: " + codedString);
// and its translation decoded
System.out.println("And its translation back is: " + ceasar.decode(codedString));
}
/**
* Used to test with all the letters of the alphabet that all the shifts work
*/
private static void testAllShifts() {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
// test the 26 possible shifts... and a little bit more just to make sure that
// our modulo operation works correctly
System.out.println(" " + alphabet);
for(int i = 0; i < 30; i++) {
Ceasar ceasar = new Ceasar(i);
System.out.format("Shift: %02d -> %s\n", i, ceasar.encode(alphabet));
}
System.out.println();
}
}

This code looks good pbl! I like the concept of starting with a console application and then moving to a GUI application.

Seperating the GUI from the business rules is the first concept that you should learn in GUI programming. No need to have each of them interfere with the other.
Every class should have it's main() method for unit test.

When you know that your business class works, here the console application, no need when you build your GUI to go back to it if something goes wrong

I also though of replacing the JRadioButton by a JComboBox but thought that the JRadioButton would show more GUI concepts

I am not attacking you. Your code obviously works. You did a great job with it, but my code is just better and easier to understand. Now someone can learn how to do it the less efficient way and the more efficient way.

Sorry to dig up old topics but I have been reading about Cryptology and decided I wanted to take a swing at creating my own simple cipher/decipher program and I was looking for a comparison. I just have a few questions.

1. How can this character array be declared without the 'new char[];' at the end?

char[] digit = clear.toLowerCase().toCharArray();

2. I am utterly confused with how this wraps the alphabet how the Ceaser cipher is used.

if(digit[i] > 'Z') {
digit[i] -= 'Z';
digit[i] += ('A' - 1);
}

I would assume that digit[0] = 'A'? and digit[3] = 'D' but I'm almost certain that is incorrect which is why i'm stumped that an integer value is being subtracted by a character. Also, the next line which has ('A' - 1) is equally as offensive to my eyes.

Thank you in advance for the response and once again sorry to rehash old topics, but I am interested in this one!

1) Note that type name; is a declaration, while type name = value; is an assignment. In this case, char[] c = new char[length]; has both a declaration (the left hand side) and assignment (the right hand side). It is legal to assign to c any char[] that has been declared elsewhere, which is what is happening in pbl's code.

2) Each character of type char has a numerical value associated with it. The letters 'A', ..., 'Z' are consecutive elements. So if the input characters are all between 'A', ..., 'Z', then adding three and subtracting 'Z' and adding in 'A' - 1 if they go over will ensure they stay within this range.

Well the second part is clear to me now, I was very confused. Although i'm still confused by the declaration of the character array called digit. I've been looking through the code and I have not found where it was declared beforehand, only the assignment of a value. In fact, I used the find hotkey and could not find it before the line I posted previously with the assignment to clear.toLower... I'm only confused by this because I tried to make a much, much simpler program using an array and received an error for not assigning a value at the end when I coded

char[] monoalph = new char[];

.

To avoid confusion I am asking why new char[] is never declared. Sorry if i'm asking a question you just answered, but the clarification would be appreciated.