Home CPSC 220

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


General Requirements


Example Files

Plain TextCipher TextKey
plain1.txtcipher1.txtannie
plain2.txtcipher2.txteagle

Submitting

To submit your program, email the code to ifinlay@umw.edu.

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.