Course Links

Resources

External

Third time is the charm: for this homework you will write the one-time pad encipher/decipher program in C. Although we've spent less time on C than OCaml or Python, you come to this with more experience, and I'm providing a bit more guidance. You have already written this program in two other languages, one imperative and one functional, so you are familiar with the operations that must be handled. For this assignment the new challenges will be working with C memory management and organizing the compilation. You have already practiced these tasks in previous homeworks and labs.

You may work with a partner for this assignment if you adhere to the guidelines.

Specification

You will write two functions (possibly with additional helpers) and a set of tests to make sure that they are working properly. The call signatures of the functions are shown below.

char* encipher(char* message, char* pad);
char* decipher(char* message, char* pad);

You should copy these call signatures into a file called cipher.h. Then create another file called cipher.c, where you will fully define the functions. Finally, you should create a file named testCipher.c containing a main function that tests your two implementations. Note that the testing program will need to #include "cipher.h" and #include "test.h", and will be compiled together with cipher.c and test.c.

Your submitted files should additionally include a makefile with rules to develop three executable programs: encipher, decipher, and testCipher that will operate as described below.

encipher message.txt pad.txt

When called from the command line, this program will read message.txt and pad.txt (see below) and output an encoded version of the message.

decipher coded.txt pad.txt

When called from the command line, this will read coded.txt and pad.txt (see below) and output a decoded version of the message.

testCipher

This will run the self-contained tests you wrote for your cipher functions. You can use this during development.

For the first two programs, I am providing a file named cipherFile.c. Compiling this with your already-written cipher.c will make the first two programs. Here are the compile commands:

gcc -o encipher cipher.c cipherFile.c
gcc -o decipher cipher.c cipherFile.c -DDECIPHER

You should put these into your makefile with the appropriate dependencies.

Testing

When you write tests for your functions, you will need to compare two strings. We haven't studied the string processing library in C, so I am giving you a starter file with two example tests. You should be able to write additional test cases using these as inspiration.

For this assignment you do not need to test cases where the pad is too short. Assume that there are always enough pad characters to encode or decode your message.

Hints

To help make this assignment easier, I can provide a line-by-line pseudocode of the helper function shiftMessage from my own solution. You are not required to follow this, but you may find it useful. As with the OCaml assignment, completing this homework without using the help file is worth one point on the grading rubric. Do not open the hint file unless you intend to use it.

To Submit