CSC 533: Organization of Programming Languages
Spring 2008

HW4: Imperative Programming in C

For this assignment, you are design and implement a C program that can be used to encrypt and decrypt files. Your program will use a rather unique but secure scheme to encrypt/decrypt files. A single source file will be encrypted as two different files, each of which is meaningless by itself but which can be combined to recreate the source file.

Since the first file is totally random and the second depends upon the randomly chosen characters in the first, neither file by itself provides any meaningful information about the original source file. Thus, if someone obtained access to only one of the files, the content of the original source file would still be secure.

The choice of the XOR operator (as opposed to a different operator such as AND or OR) is instrumental in making the scheme work. It turns out that the XOR operation has a convenient reversibility. If you are given the XOR of two numbers and either one of those numbers, then you can XOR them to get the other number. That is,

Let C = A ^ B (where ^ is the XOR operator). Then A ^ C = B and B ^ C = A.

Thus, decrypting a file using this scheme simply involves taking the XOR of each of the corresponding characters in the two encrypted files. The XOR of the first characters from these two files will give you the first character from the original source file, the XOR of the second characters will give you the second character from the source file, and so on.

Your program should allow the user to either encrypt or decrypt files using this scheme. In either case, the user should be prompted for the names of the files (either the source file and two output files in the case of encryption, or the two encrypted files and merged output file in the case of decryption). For an example of how file processing is done in C, be sure to look at the files.c example that was added to the lecture slides. You may also need to do a little research into other C library functions, such as srand and rand (from stdlib.h).

Note: if you are using Visual Studio (or .Net) to create and execute your C program, be careful to use only C language features. For example, #include is legit, but #include (which loads the C++ string library) is not. When compiling a C program using a C++ compiler, you may receive some warnings about deprecated function calls (e.g., fopen), but these can be ignored.