======Project: SECRET AGENT====== A project for C++ by Michael during the Spring 2012. This project was begun on feb 14 and is anticipated to take 2 weeks to complete. Project was completed on Feb 28th, 2012 =====Objectives===== We are making an encoder and decoder in C. =====Prerequisites===== In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved: * successful completion of project #0 and solid understanding of pertinent topics * familiarity with memory allocation via **malloc(3)** * familiarity with looking up C function parameters/information in the manual * familiarity with if statements, especially in use for error detection * familiarity with pointers and pointer arithmetic * familiarity with loops * file I/O =====Background===== Writting a program that will encode a text file by chagning the characters.\\ A different file will decode a text file by reversing the change in characters. \\ It asks for a cypher key, and you will need to know that key when you decode it. =====Scope===== This project will test your familiarity with more involved algorithms, storage, and control structures. Encoding is the process of converting a message into a coded form. Decoding is the reverse- retrieving that original message from an encoded form. In this project you will write a 2 programs: one that will take a plain text message and convert it into a coded form (largely indecipherable to the regular english expecting eye) using a given cipher key, and another that will reverse the process (or decipher), when given the appropriate key. The key is a numeric used to rotate the alphabet a set amount. Where A would normally equal 1, and Z 26, using a cipher key of 1 to shift 1 position to the right; A would now equal 2 (or B), B is now 3 (or C), and Z would be 1 (A). Your encoding program can operate as follows: * obtains its cipher key from a text file called "key.txt" * or, if you prefer, use command-line arguments to provide the key * obtains the input message from a file called "message.txt" * if "plain.txt" is blank, or does not exist, the program should prompt the user to enter the message via STDIN * outputs the ciphertext message to STDOUT **AND** saves it to a file called "cipher.txt" * implement error checking to avoid segfaults The decoding operation is essentially the reverse (shifting to the left). I'd suspect you could reuse much of the same logic. Your decoding program: * obtains its cipher key from a text file called "key.txt" * or, if you prefer, use command-line arguments to provide the key * obtains the input cipher from a file called "cipher.txt" * if "cipher.txt" is blank, or does not exist, the program should prompt the user to enter the message via STDIN * outputs the plaintext message to STDOUT **AND** saves it to a file called "plain.txt" * implement error checking to avoid segfaults If you want, you may implement both functionalities into one program so long as you provide a mechanism for the user to access both operations (but note this is not required for successful completion of this project). =====Code===== The encipher code: /*encodes*/ #include #include int main() { FILE *original, *encoded; int key = 0; //number of asci spaces moved int holder = 0; //used as a temp space holder char letter; original = fopen("file2.txt","r"); encoded = fopen("encoded.txt","w"); printf("Please pick your encyphering number: "); scanf("%d",&key); letter = fgetc(original); while(letter != EOF) { if ((letter >='A') && (letter <= 'Z')) { holder = letter - 65; holder = holder + key; holder = holder % 26; letter = holder + 65; } if ((letter >= 'a') && (letter <= 'z')) { holder = letter - 97; holder = holder + key; holder = holder % 26; letter = holder + 97; } fprintf(encoded, "%c", letter); letter = fgetc(original); } printf("Encoding complete\n"); fclose(original); fclose(encoded); return(0); } Deciphering code. //decodes #include #include int main() { FILE *encoded, *decoded; char typed; int key = 0; char letter; unsigned char holder = 0; encoded = fopen("encoded.txt", "r"); decoded = fopen("decoded.txt", "w"); printf("Please enter the original key number: "); scanf("%d", &key); letter = fgetc(encoded); while(letter != EOF) { if ((letter >= 'A') && (letter <= 'Z')) { holder = letter - 65; holder = holder - key - 230; holder = holder % 26; letter = holder + 65; } if ((letter >= 'a') && (letter <= 'z')) { holder = letter - 97; holder = holder - key - 230; holder = holder % 26; letter = holder + 97; } fprintf(decoded, "%c", letter); letter = fgetc(encoded); } printf("Decoding complete, check decoded.txt \n"); fclose(encoded); fclose(decoded); return(0); } =====Execution===== An example run of the enciphering process: Please pick your encyphering number: 4 Encoding complete Example run of the decoding process: Please enter the original key number: 4 Decoding complete, check decoded.txt =====Reflection===== I learned that this project was a pain in the butt. I also know, I don't want to mess with stuff like this again. It gave me a headache trying to figure it out, and I don't think I could easly rewrite this program. =====References===== In this project, I had a lot of help from Matt, our teacher. I had so many issues that I couldn't figure out why they were happening.