CS 11 -- Lab 7

The result of this lab will be a program that can encrypt or decrypt a file. Mind you, the encryption won't be all that secure, but that's not the point...


A simple encryption scheme

Given an original text file (what we will call the cleartext), we want to produce a version of the file with the characters scrambled (a ciphertext). In that way, we can delete the cleartext, leaving only the encrypted ciphertext that people cannot read. Of course, we may, at some point, want to recover the original file. So, we want also to be able to decrypt the ciphertext back into its original cleartext.

We will use a simple encryption scheme known as a Caesar cipher. Specifically, enumerate the cleartext characters. For example, if we consider only capital letters of the English alphabet, then A is character 1, B is character 2, etc.

To encrypt a cleartext character, replace it with kth successor (e.g. if k = 3, then 1 + k = 4, and thus A is transformed into a D). Characters at the end of the alphabet "wrap around" -- that is, Z is character 26, and 26 + k = 29 mod 26 = 3, or C.

Decryption reverses this transformation. For each ciphertext character, replace it with its kth predecessor in the alphabet (e.g. D is replaced with A, etc.).


Your assignment

Write a program named CaesarCipher (that is, create the CaesarCipher class in a file named CaesarCipher.java). This program should accept 4 command-line parameters, in the following order:

  1. Function: The string encrypt indicates that the program should perform encryption, while decrypt will indicate decryption.
  2. Shift value: By how much should the character values be shifted? (That is, what value of k should be used?)
  3. Input filename: The name of a file from which to read the input cleartext (encryption) or ciphertext (decryption).
  4. Output filename: The name of a file to which to write the output ciphertext (encryption) or cleartext (decryption).

The alphabet here is the set of 256 characters used for standard English text. (This set includes all manner of punctuation marks and control characters).

To test your code, there are three files that you should copy from my directory:

Use these files to ensure that if you encrypt and then decrypt a file, the result is a file that matches the original file. You can have the machine compare the files automatically using the diff command. For example, the following sequence of commands should encrypt and then decrypt small.txt, shifting the characters by 15 alphabetic positions, and then compare the original to the decrypted file:

java CaesarCipher encrypt 15 small.txt small.cipher
java CaesarCipher decrypt 15 small.cipher small.clear
diff small.txt small.clear

If the files match exactly, then diff produces no output (that is, no news is good news). Otherwise, it emits (in a somewhat funny format) a listing of the lines that do not match.

Note that it is critical that your program accept the command line arguments exactly as described above. I will be testing your program using another program of my own, and mine will assume that you've implemented the interface correctly.


Submit your work

Submit, as lab-7, your CaesarCipher.java file using the cs11-submit command.


This assignment is due at 11:59 pm, Thursday, 1-December-2005

Scott F. Kaplan
Last modified: Fri Nov 18 07:59:16 EST 2005