CMSC 201
Programming Project Four
Cryptography

Out: Sunday 11/10/02
Due: before Midnight, Sunday 11/24/02

The design document for this project, design4.txt
is due: Before Midnight, Sunday 11/17/02

The Objective

The objective of this assignment is to give you practice using command line arguments, pointers, files, malloc, recently introduced I/O functions, characters and strings.

The Background

Cryptography is the science of encoding and decoding messages. Encoding a message changes the message so that only the sender and receiver of the message can understand it. If the message is seen by anyone else, it will be unreadable. The sender "encodes" the message, the receiver "decodes" the message.

A simple encoding method is letter substitution. Each alphabetic character in the message is replaced with different letter from the alphabet. For example, we might substitute the letter that comes next in the alphabet. Then each 'a' would be replaced with a 'b', each 'b' with a 'c', and so on. The letter 'z' would be replaced with an 'a'. Using this scheme, the word "help" would be encoded as "ifmq". Any characters which are not alphabetic are not translated.

Both the sender and receiver must know how to translate the characters.

The Task

Your task is to write a program that encodes and decodes files of text using simple letter substitution.

Your program will accept one command line argument which is the name of the file that contains the translation table for encoding and decoding with letter substitution. The file is described in more detail below.

Your program will present a menu to the user with the following choices

E - Encode a file D - Decode a file T - Print the translation table Q - Quit When the user chooses to encode a file, your program will prompt the user for the name of the file to be encoded and the name of the output file into which the encoded message is written. Your program must dynamically allocate, using malloc(), the space needed to hold the contents of the input file in a string, read the contents of that file into the string, display the original message, encrypt the string, and display the encoded message to the user as well as write it to the output file.. The number of characters which were translated and the total number of characters processed are reported when the encryption is finished.

When the user chooses to decode a file, your program will prompt the user for the name of the file to be decoded. Your program must dynamically allocate, using malloc(), the space needed to hold the contents of the input file in a string, read the contents of that file into the string, display the encoded message, decrypt the string, and display the decoded message. As for encryption, your program will report the number of characters translated and the total number of characters processed.

To print the translation table, show which letter is substituted for every letter of the alphabet.

When the user quits, your program will report the total number of files encoded/decoded, the total number of characters processed in all files and the total number of characters translated in all files.

See the sample output below for details.

Project Requirements

  1. In this project, your code MUST include the following functions. You MAY NOT change the function prototypes in any way. A good design will require other functions as well.
  2. Only alphabetic characters should be translated. UPPERCASE characters must be encoded and decoded as UPPERCASE. Lowercase characters must be encoded and decoded as lowercase. All other characters,including punctuation and spaces must be unchanged when encoded or decoded.
  3. Your program must accept both uppercase and lower case input for the menu selection. You must validate the menu choices.
  4. You MAY NOT use scanf(), fscanf(), or sscanf() in this project.

Assumptions

Your program may make the following assumption
  1. All filenames will be a maximum of 50 characters.
  2. Graders will enter only one character and the newline for the menu choice.

The Translation Table File

The command line argument for your program is the name of the file that contains the translation table for the letter substitution. The file contains the 26 letters of the alphabet in scrambled order. Read the entire contents of this file into your program.

The first letter in the file should be substituted for the letter 'a'. The second letter in the file should be substituted for the letter 'b', the third letter for 'c' and so on. The last letter in the file is substituted for 'z'.

The letters in the translation table file are all lowercase. If there is an uppercase letter to be translated, it should be replaced with the uppercase version of the letter to be substituted (retain case).
I.e., if the first letter in the file is 'w', then substitute 'w' for 'a' and 'W' for 'A'.

Data Files

Several data files are available for you to use for testing. In Bogar's public directory /afs/umbc.edu/users/s/b/sbogar1/pub/ you will find the following files. You may copy them to your directory for testing.
  1. code1.dat and code2.dat that each contain a translation table as described in the section above
  2. song1.dat that contains the first verse of a song. This file was encrypted (and so must be decrypted) with code1.dat
  3. song2.dat that contains the first verse of the same song. This file was encrypted (and so must be decrypted) with code2.dat
  4. sayings1.dat that contains a few well-known sayings. This file was encrypted (and so must be decrypted) with code1.dat
  5. sayings2.dat that contains the same well-known sayings. This file was encrypted (and so must be decrypted) with code2.dat
  6. test.dat which contains a well known phrase that contains every letter of the alphabet. It is not encrypted.
You may use these files or create your own files for testing.

Sample Output

linux1[101] a.out code1.dat Your Greeting Goes Here E - Encode a file D - Decode a file T - Print the translation table Q - Quit Please enter menu selection : e Please enter input file name (50 chars max): test.dat Please enter output file name (50 chars max): test.out Start Encryption Original Text: The quick brown fox jumps over the lazy dog. Encrypted Text: Qru ksamd oliwh tix bsgjn ivul qru fczy pie. Encryption complete 35 characters encoded 44 total characters E - Encode a file D - Decode a file T - Print the translation table Q - Quit Please enter menu selection : d Please enter input file name (50 chars max): test.out Start Decryption Coded Text: Qru ksamd oliwh tix bsgjn ivul qru fczy pie. Decoded Text: The quick brown fox jumps over the lazy dog. Decryption complete 35 characters decoded 44 total characters E - Encode a file D - Decode a file T - Print the translation table Q - Quit Please enter menu selection : T c o m p u t e r a b d f g h i j k l n q s v w x y z E - Encode a file D - Decode a file T - Print the translation table Q - Quit Please enter menu selection : t c o m p u t e r a b d f g h i j k l n q s v w x y z E - Encode a file D - Decode a file T - Print the translation table Q - Quit Please enter menu selection : M M is not a correct response Please, try again E - Encode a file D - Decode a file T - Print the translation table Q - Quit Please enter menu selection : q Processing Summary 2 files translated 70 characters translated 88 total characters processed linux1[102]

Although your output need not be identical to the above, all information (including the greeting) must be present.

Submitting the Program

Use the submit command as in previous projects. Make sure you submit all files necessary for your project to compile.