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 the String during the program as you cipher and encipher.
You'll want to set up the Vigenère cipher in a two-dimensional array of Strings. The syntax is:
You can use the String mentioned above in populating the array.
I used the Command window (DOS window) for input/output. Here is how you can set this up:
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.
The 'charAt' method of the String class returns 1 character. For example:
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.
To find the integer associated with a given character, use an int cast. For example, the code
If the phrase you're dealing with is longer than the key word, you'll cycle through the key word a few times. In thinking about how to do this, consider modular arithmetic. Calculate these values:
3 % 5
4 % 5
5 % 5
6 % 5
Would this be useful if the key word was 5 characters long?
Testing Your Cipher
Try the following examples to test your cipher code.
|MESSAGE||CIPHER KEY||ENCIPHERED MESSAGE|
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".