Vigenère Cipher
Due: October 19
Objective
To use strings and ASCII characters in Java and continue practicing programming.Task
For this assignment, you will write a program to encode and decode messages using the Vigenère cipher.
The Vigenère cipher is a simple method for producing secret codes. It is based on taking a message, and a key, and rotating each letter of the message by each letter of the key. This is done by adding the values according to the ASCII table. If the key is shorter than the message (which is typical), then the key is rotated around. For example, if the message is "hello", and the key is "abc":
h (104) e (101) l (108) l (108) o (111)
+ a (97) b (98) c (99) a (97) b (98)
----------------------------------------
201 199 207 205 209
We have added the ASCII values of the key into the ASCII values of the message. Now, however, the values we get are not valid ASCII codes for normal characters.
For our purposes, "printable characters" are those from '!' (ASCII code 33) to '~' (ASCII code 126). Because these values are greater than the upper limit of 126, we need to subtract the range of possible values from it. This value is 126 - 33 = 93:
201 199 207 205 209
- 93 93 93 93 93
----------------------------------------
l (108) j (106) r (114) p (112) t (116)
So the encoded message is "ljrpt".
To decode it, we'd do the reverse, and subtract each letter of the key from the encoded message:
l (108) j (106) r (114) p (112) t (116)
- a (97) b (98) c (99) a (97) b (98)
-----------------------------------------
11 8 15 15 18
Now we have to wrap these values into the range of printable characters, by adding 93 to the values until they are in the range of 33 to 126:
Then wrap until greater than or equal to ! (33)
Again, do this by adding the range (93).
11 8 15 15 18
+ 93 93 93 93 93
----------------------------------------
h (104) e (101) l (108) l (108) o (111)
Hence we get our original message "hello" back out!
Only by encoding and decoding using the same key can we recover the message. This cipher would let you communicate without most people being able to know what the message is (but it would be easy for a professional to break).
You should write a program that will ask the user whether they want to do an encode or decode operation. Then it should ask the user what file their message is in, and then ask the user for the key.
It should then compute the new message (by either encoding or decoding, based on the user's choice). The program should then, ask the user for an output file name and print the new message to that file.
If the original message has spaces in it (ASCII code 32), they should be not be changed in the encoded/decoded message, but should remain as spaces!
Details
- Ask the user if they would like to encode or decode a message. You should repeatedly ask them until they choose one of those two options.
- Read in the input file name. If the file is not found, you should print an error message and end the program.
- You should then read the message in from the file. You can assume it will all be on one line.
- You should then read the key in from the user (not a file). You can assume the key will have no spaces.
- Perform the encoding or decoding using the algorithm detailed above.
- Ask the user for the name of the output file.
- Lastly, print out the resulting string in the file they specified.
- After adding the key to the message, but before shifting it, it's possible that the number could be bigger than 127, which is the maximum size for a char. Thus, make sure you store the intermediate results in int variables!
- You should test your program and make sure that it can encode messages, then decode them to get the original messages back!
General Requirements
- Your code should be readable and reasonably indented.
- You must provide comments in your program.
- You must include a comment at the top with your name.
- You should not have a "package" line in your program.
- You should not have the comments NetBeans puts in by default e.g. the "license header", "@author" or "TODO" lines.
- Your file name/class name should be something descriptive, not something like "JavaApplication4".
Example Files
| Plain Text | Cipher Text | Key |
| plain1.txt | cipher1.txt | annie |
| plain2.txt | cipher2.txt | eagle |