### Vigenère Cipher

16 points

Caesar Cipher
To use a Caesar cipher, replace each letter in the message with another letter. It uses two rows of letters:

 A B C D E ... J K L M N ...

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.

Vigenère 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]

Enciphering
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.

Deciphering
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.

Coding
You can set up a String that consists of the 26 letters of the alphabet, followed by the 26 letters of the alphabet again:
String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";

You can then access alphabets to help set up your Vigenère cipher. For example,
alphabets.substring(0, 26)
would give you "ABCDEFGHIJKLMNOPQRSTUVWXYZ", the first row of the cipher.
alphabets.substring(1, 27)
would give you "BCDEFGHIJKLMNOPQRSTUVWXYZA", the second row of the cipher.

You'll want to set up the Vigenère cipher in a two-dimensional array of Strings. The syntax is:
private String[][] table = new String[26][26];

I used the Netbeans output window for input/output. Here is how you use the output window in your program:
Scanner scan = new Scanner(System.in);
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:
String day = "MONDAY";
char ch = day.charAt(4);
System.out.println("ch: " + ch);

2. Use Character.toString() to convert a char to a String:
String uz = Character.toString(ch);
System.out.println("uz: " + uz);

3. Use an int cast to convert a char to its ASCII integer:
int green = (int)ch;
System.out.println("green: " + green);

4. Use a char cast to convert an int to its ASCII character:
char today = (char)green;
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:
ch = 'A';
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:
ch: A
uz: A
green: 65
today: A
position: 0
position: 1

Try the following examples to test your cipher code.
 ENCIPHERING EXAMPLES MESSAGE CIPHER KEY ENCIPHERED MESSAGE TARGETISMIDWAYISLAND MONDAY FOEJERUGZLDUMMVVLYZR GENERALLEEISSICK GETTYSBURG MIGXPSMFVKOWLBAC DECIPHERING EXAMPLES TINKVSGUVKBCTWLTHAA BEACH ? KUZEELMXSZYXNICDPIES COLLEGE ?

Starting Point