CPSC 120 Program 4
Due Friday, Dec 6

The Caesar Cipher

In this assignment you will write a class Crypt that has methods to encrypt and decrypt text using a Caesar cipher. In the Caesar cipher, each character in the original text ("plaintext" in cryptography parlance) is shifted by a fixed amount. So if the plaintext is HELLO and the shift is 3, the encrypted text is KHOOR. If the end of the range of characters is reached during the shift, just wrap around to the beginning; so if you're dealing only with capital letters, YOU becomes ARX under a shift of 3. Decryption is just the reverse of encryption; to decrypt text that was shift by 3, just shift it by -3. But since it wraps around, every negative shift has a corresponding positive shift -- for a range of 26 characters such as the capitals-only example, a shift of -3 is the same as a shift of +23.

Of course, restricting the range to only capital letters is quite limiting. It is also unnecessary; we could choose any range of the Unicode character set to deal with. Most of the common printable Unicode characters have codes in the range 32..126 (this includes upper and lower case letters, digits, space and most punctuation), so we'll use that range. In general to shift a character in the range LO..HI (inclusive) you would do the following:

       (char) ((c-LO + shift)%((HI-LO+1) + LO)
This wraps around appropriately while staying in the given range.

Program Structure

You will need the following methods in your Crypt class. Your Crypt class is essentially an encryption machine that just performs the tasks given to it without maintaining any state, so all of these methods will be static. You will also need to write a TestCrypt class that prompts the user for a phrase, displays the encrypted phrase, then asks if the user would like to try to decrypt it. If the response is yes, the program should attempt to decrypt it as described above; if the response is no, the program should terminate.

Input and Output

All I/O should be done using dialog boxes. Remember that when using dialog boxes you have to use Ctrl-C to get the prompt back when your program ends.

What to Turn In

Turn in hardcopy of your Crypt and TestCrypt classes. Tar your prog4 directory and e-mail it to me with cpsc120 prog4 in the Subject line.