This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:brobbin4:portfolio:cprogproject1 [2012/02/26 06:58] – [Code] brobbin4 | user:brobbin4:portfolio:cprogproject1 [2012/02/29 15:38] (current) – [References] brobbin4 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for CSCS1320 C\C++ Programmint by Brian Robbins during the Spring 2012 Semester. | ||
+ | |||
+ | This project was begun on February 20th, 2012 and is anticipated to take 1 week to complete. Project was completed on February 25, 2012. | ||
+ | |||
+ | =====Objectives===== | ||
+ | The purpose of this project was to put to use all of the things we have learned in class thus far. When this project has reached completion I will have two programs. The first program will cipher a message contained within a text file with a cipher key found in another text file. This key is actually used to figure out the number of shifts that will occur durring the ciphering. The resultant of this program will be a text document that contains the ciphered message. The second program witt decipher the previously ciphered message using the same technique but in reverse essentially reversing the effects of the original ciphering program. The output from this deciphering program will be a text document with the exact same message in it as the original message file used in the original cipher program. By completing this project I hope to gain a better understanding of how if statements work, and how external files are utilized within a program. This will also help me understand basic mathmatical concepts within the c programming language. | ||
+ | |||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * 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/ | ||
+ | * familiarity with if statements, especially in use for error detection | ||
+ | * familiarity with pointers and pointer arithmetic | ||
+ | * familiarity with loops | ||
+ | * file I/O | ||
+ | |||
+ | =====Background===== | ||
+ | The idea for this project is to create a program or programs that will cipher and decipher a message contained within a text document. | ||
+ | =====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 " | ||
+ | * or, if you prefer, use command-line arguments to provide the key | ||
+ | * obtains the input message from a file called " | ||
+ | * if " | ||
+ | * outputs the ciphertext message to STDOUT **AND** saves it to a file called " | ||
+ | * 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 " | ||
+ | * or, if you prefer, use command-line arguments to provide the key | ||
+ | * obtains the input cipher from a file called " | ||
+ | * if " | ||
+ | * outputs the plaintext message to STDOUT **AND** saves it to a file called " | ||
+ | * 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: | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | * encipher.c - program that encodes a message according to a key | ||
+ | * | ||
+ | * | ||
+ | * Compile with: gcc -o encipher encipher.c | ||
+ | * | ||
+ | * Place key value in: key.txt | ||
+ | * Place message to encipher in: message.txt | ||
+ | * Enciphered message placed in: cipher.txt | ||
+ | * | ||
+ | * Execute with: ./ | ||
+ | * | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | FILE *key, *message, *cipher; | ||
+ | int code; | ||
+ | long long int c; | ||
+ | char fname[]= " | ||
+ | |||
+ | key=fopen(" | ||
+ | message=fopen(fname," | ||
+ | cipher=fopen(" | ||
+ | |||
+ | if(key == NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | if(message == NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | if(argc == 2) | ||
+ | { | ||
+ | printf(" | ||
+ | code = atoi(*(argv+1)); | ||
+ | } | ||
+ | |||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | fscanf(key, | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | c = fgetc(message); | ||
+ | while(c != EOF) | ||
+ | { | ||
+ | |||
+ | if((c >= ' | ||
+ | { | ||
+ | c = c + code; | ||
+ | while(c > 77) | ||
+ | { | ||
+ | c = c - 13; | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | else if((c >= ' | ||
+ | { | ||
+ | c = c - code; | ||
+ | while(c < 78) | ||
+ | { | ||
+ | c = c + 13; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if((c >= ' | ||
+ | { | ||
+ | c = c + code; | ||
+ | while(c > 109) | ||
+ | { | ||
+ | c = c - 13; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | else if((c >= ' | ||
+ | { | ||
+ | c = c - code; | ||
+ | while(c < 110) | ||
+ | { | ||
+ | c = c + 13; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | fprintf(cipher, | ||
+ | fprintf(stdout, | ||
+ | c = fgetc(message); | ||
+ | } | ||
+ | |||
+ | fclose(message); | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The decipher code: | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | * decipher.c - program that decodes a message according to a key | ||
+ | * | ||
+ | * | ||
+ | * Compile with: gcc -o decipher decipher.c | ||
+ | * | ||
+ | * Place key value in: key.txt | ||
+ | * Place message to decipher in: cipher.txt | ||
+ | * deciphered message placed in: plain.txt | ||
+ | * | ||
+ | * Execute with: ./ | ||
+ | * | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | FILE *key, *message, *decipher; | ||
+ | int code; | ||
+ | long long int c; | ||
+ | char fname[]= " | ||
+ | |||
+ | key=fopen(" | ||
+ | message=fopen(fname," | ||
+ | decipher=fopen(" | ||
+ | |||
+ | if(key == NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | if(message == NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | if(argc == 2) | ||
+ | { | ||
+ | printf(" | ||
+ | code = atoi(*(argv+1)); | ||
+ | } | ||
+ | |||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | fscanf(key, | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | c = fgetc(message); | ||
+ | while(c != EOF) | ||
+ | { | ||
+ | if((c >= ' | ||
+ | { | ||
+ | c = c - code; | ||
+ | while(c < 65) | ||
+ | { | ||
+ | c = c + 13; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | else if((c >= ' | ||
+ | { | ||
+ | c = c + code; | ||
+ | while(c > 90) | ||
+ | { | ||
+ | c = c - 13; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if((c >= ' | ||
+ | { | ||
+ | c = c - code; | ||
+ | while(c < 97) | ||
+ | { | ||
+ | c = c + 13; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | else if((c >= ' | ||
+ | { | ||
+ | c = c + code; | ||
+ | while(c > 122) | ||
+ | { | ||
+ | c = c - 13; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | fprintf(decipher, | ||
+ | fprintf(stdout, | ||
+ | c = fgetc(message); | ||
+ | } | ||
+ | |||
+ | fclose(message); | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | =====Execution===== | ||
+ | |||
+ | An example run of the enciphering process: | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | |||
+ | Enciphering process initiated. | ||
+ | No argument was provided durring execution. Geting cipher key from ' | ||
+ | Cipher key is 18246975 | ||
+ | Enciphering the message contained within ' | ||
+ | Nabz wyvckin qgz g yvsge wgbu nv lbmoyk von! | ||
+ | Enciphering complete! Enciphered message is stored in cipher.txt. | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | An example run of the deciphering process: | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | |||
+ | Deciphering process initiated. | ||
+ | No argument was provided durring execution. Geting cipher key from ' | ||
+ | Cipher key is 18246975 | ||
+ | Deciphering message contained within ' | ||
+ | This project was a royal pain to figure out! | ||
+ | Deciphering complete! Deciphered message is stored in plain.txt. | ||
+ | lab46: | ||
+ | </ | ||
+ | =====Reflection===== | ||
+ | This project was alot tougher than originally thought. I thought the shift thing would be really easy but it actually took research and colaboration from other class mates to be able to complete this project. I now completely understand ' | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | * Lab46 Class chat | ||
+ | * Matt's hint | ||
+ | * C Pocket Reference | ||
+ | * Fellow classmates |