Lab 7: Vigenère Cipher
Due by the end of class
The objective of this lab is to implement the Vigenère Cipher. In the Vigenère Cipher, we use a special keyword to encrypt a message. We represent each letter of the alphabet as a number from 0-25. In order, we add each letter from the keyword to the message and mod by 26. If the keyword is shorter than the message, we simply repeat the keyword.
For example, lets say that the message is HOWSTUFFWORKS
and the keyword is CIPHER
. The following table shows how we can find the final version of the ciphertext, borrowed from How Stuff Works.
Plain Text: | HOWSTUFFWORKS |
Keyword: | CIPHER |
Cipher Text: | JWLZXLHNLVVBU |
Your mission is to write a program that will read a message and a keyword and apply the Vigenère Cipher to it.
Specification
Create a project called Lab7
. Add a class called Vigenere
. Please spell Vigenere correctly, because of my automated grading tools.
Your main()
method should prompt the user for some plain text. Then, it should prompt the user for a keyword. You may assume that both the plain text and the keyword contain no spaces and are only made up of upper case letters.
To perform the encryption, you need to loop through the plain text and add each letter from the keyword to it, mod 26. Unfortunately, neither the letters in the plain text nor in the keyword have values between 0 and 25. You will have to convert those letter values so that they are between 0 and 25, then do the add, then do the mod, then convert them back to the proper ASCII values. Note: You should never use the numerical value of an ASCII character to do these conversions. If you wish to add the value of the letter A, add 'A'
.
You only need one loop for this program. However, the plain text is generally longer than the keyword. So, though you will only need one loop, you need to keep track of where you are in the keyword independently of the plain text. Whenever you get to the end of the keyword, you should set that index back to 0.
Sample Output
Here is an example of sample output for plain text ATTACKATDAWN
and keyword LEMON
. Your output should match as closely as possible.
Enter your plaintext: ATTACKATDAWN Enter your keyword: LEMON Your cipher text is: LXFOPVEFRNHR
Turn In
Turn in your code by uploading Vigenere.java
from the Lab7\src
folder wherever you created your project to Blackboard. Do not upload the entire project. I only want the Vigenere.java
file.
All work must be done individually. Never look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.