### 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.

• The first of the encrypted files will be totally random. For each character in the source file, you will select a random character and write it to the new file. Thus, this new file will have the same length as the source file, but will consist of totally random characters.

• The second of the encrypted files is a combination of the source file and the file of random characters. In particular, each character in this second file will be the bitwise exclusive-or (XOR) of the corresponding characters in the source file and the random file. That is, the first character in this file is the XOR of the first character in the source file and the first character in the random file. The second character is the XOR of the second characters from the source and random files, and so on.

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.