To use a Caesar cipher, replace each letter in the message with another letter. It uses two rows of letters:
The first row has the first 5 letters of the alphabet. The letters in the second row have been shifted 9 spaces. If you need to write an A, find the A in the first row, and then look below it to the second row and find the J. In the enciphered message you would write a J to represent the A. If you need to write a B, you would write a K. And so on. The shift can be anything between 1 and 25 spaces. Click here for more information on the Caesar cipher.
The Vigenère cipher is a more complex cipher that builds on the Caesar cipher. The Vigenère cipher has 26 rows, and each row contains a Caesar cipher with a different shift. It looks like this:
[Image created by Brandon T. Fields]
The Vigenère cipher makes use of a key word in the process of enciphering or deciphering a message. Say that the key word is MONDAY. And the message that is being enciphered is TARGETISMIDWAYISLAND. Begin with the T of the message. Find the first letter of the key word - M. Look in the row of the Vigenère cipher that begins with M. Find the letter in this row that has T at the top of the column. In other words, find the letter at position (M, T). This letter is F. So in place of a T in the enciphered message, put an F.
The second letter of the message is A. The second letter of the key is O. Find the letter in the cipher that begins with O, and has A at the top of the column. This letter is O. So in place of A in the enciphered message, put an O. And so on. The message may be longer than the key. After you're enciphered TARGET, you've used every letter of the key word. In that case, just start over at the beginning of the key word. To encipher the I after TARGET, use M from the key word.
When deciphering, you're reversing the enciphering process. Let's say that the message is LPHSE and the key word is PIZZA. Look for L in the row of the table that begins with P. Then identify the letter at the top of the column; that is the deciphered letter, which here is W. So the first letter of the deciphered word is W.
Continue the process. If you use all the letters in the key word, just start over.
You can set up a String that consists of the 26 letters of the alphabet, followed by the 26 letters of the alphabet again:
You can then access
You'll want to set up the Vigenère cipher in a two-dimensional array of Strings. The syntax is:
I used the Netbeans output window for input/output. Here is how you use the output window in your program:
System.out.println("WHATEVER YOU WANT TO SAY HERE");
String pwd = scan.next();
pwd = pwd.toUpperCase();
So the way to get input from the user is with 'scan.next()'.
I recommend the last line to simplify your programming, always dealing with uppercase characters.
It might be useful to view the ASCII table, which associates every key on the keyboard with an integer. Click here to view an ASCII table. The relevant columns are headed 'Dec' and 'Chr'. Note, for example, that the ASCII value for 'A' is 65.
Some suggestions for converting between an int, a char and a String:
1. Use .charAt() to extract a char from a String:
char ch = day.charAt(4);
System.out.println("ch: " + ch);
2. Use Character.toString() to convert a char to a String:
System.out.println("uz: " + uz);
3. Use an int cast to convert a char to its ASCII integer:
System.out.println("green: " + green);
4. Use a char cast to convert an int to its ASCII character:
System.out.println("today: " + today);
5. In this program, it might be useful to associate "A" with position 0,
"B" with position 1, etc. For example, in the first row of the Vigenère cipher table, "A" is in column 0, "B" is in column 1, and so on.
Consider this code that accomplishes this:
int position = (int)ch - (int)'A';
System.out.println("position: " + position);
ch = 'B';
position = (int)ch - (int)'A';
System.out.println("position: " + position);
The output from the code in examples 1-5 above is:
Testing Your Cipher
Try the following examples to test your cipher code.
|MESSAGE||CIPHER KEY||ENCIPHERED MESSAGE|
You can download here a starting point for writing this program.
If you'd like to run a complete version of this program, download cipher.zip, uncompress it and run it. The command to run the JAR file is: java -jar myVigenereCipher.jar, which you can type into a DOS window. It will be necessary to put the file (myVigenereCipher.jar) in a folder that is on Java's classpath so that Java can see the file, or modify the classpath. When it runs, the software will ask for you a password, which is "MHS".