This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:cforman:portfolio:cprogproject1 [2012/02/26 05:39] – [References] cforman | user:cforman:portfolio:cprogproject1 [2012/02/29 04:19] (current) – [Code] cforman | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for C/C++ by Corey Forman during the Spring 2012. | ||
+ | |||
+ | This project was begun on 2/25/12 and is anticipated to take 2/24/12 to complete. Project was completed on 02/26/12 at 12:44 am. | ||
+ | |||
+ | =====Objectives===== | ||
+ | The objective of this is to push our minds to the limit as we try to use every ounce of the knowledge we have gathered up until this point. We want to make a program that enciphers and deciphers a script for secret transportation. lol but really this project will test our ability to debug and understand gcc warnings. | ||
+ | =====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===== | ||
+ | State the idea or purpose of the project. What are you attempting to pursue? | ||
+ | |||
+ | Upon approval, you'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK. | ||
+ | |||
+ | Providing any links to original source material, such as from a project page, is a good idea. | ||
+ | |||
+ | You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.) | ||
+ | |||
+ | =====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: | ||
+ | cprog is called encrypt | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | FILE *cypher, *in, *on, *encripted, | ||
+ | int key;key=0; | ||
+ | char c, d; | ||
+ | cypher=fopen(" | ||
+ | key = atoi(*(argv+1)); | ||
+ | fprintf(cypher," | ||
+ | encripted=fopen(" | ||
+ | in = fopen(" | ||
+ | on = fopen(" | ||
+ | encripted2=fopen(" | ||
+ | if (in == NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | c = fgetc(in); | ||
+ | d = fgetc(on); | ||
+ | printf(" | ||
+ | while(d != EOF) | ||
+ | { | ||
+ | fprintf(stdout," | ||
+ | d = fgetc(on); | ||
+ | } | ||
+ | printf(" | ||
+ | printf(" | ||
+ | while(c != EOF) | ||
+ | { | ||
+ | // printf(" | ||
+ | if((c >= 65) && (c <= 90)) | ||
+ | { | ||
+ | c = c + (atoi(*(argv+1))); | ||
+ | // printf(" | ||
+ | } | ||
+ | else if((c >= 97) && (c <= 122)) | ||
+ | { | ||
+ | c = c + (atoi(*(argv+1))); | ||
+ | // printf(" | ||
+ | } | ||
+ | if((c == (122 + 1)) || (c == (122 + 1))) | ||
+ | { | ||
+ | c = c - 26; | ||
+ | // printf(" | ||
+ | } | ||
+ | |||
+ | | ||
+ | // printf(" | ||
+ | fprintf(encripted," | ||
+ | printf(" | ||
+ | |||
+ | c = fgetc(in); | ||
+ | } | ||
+ | |||
+ | fclose(in); | ||
+ | fclose(on); | ||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | Deciphering code is called decrypt | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | FILE *cypher, *in, *on, *encrypted; | ||
+ | int key; key=0; | ||
+ | char c, d; | ||
+ | cypher=fopen(" | ||
+ | encrypted=fopen(" | ||
+ | in = fopen(" | ||
+ | on = fopen(" | ||
+ | |||
+ | if (in == NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | c = fgetc(in); | ||
+ | d = fgetc(on); | ||
+ | printf(" | ||
+ | while(d != EOF) | ||
+ | { | ||
+ | fprintf(stdout, | ||
+ | d = fgetc(on); | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | while(c != EOF) | ||
+ | { | ||
+ | |||
+ | if(( c>=65+ (atoi(*(argv+1)))) && (c <= 90 +(atoi(*(argv+1))))) | ||
+ | { | ||
+ | c = c - (atoi(*(argv+1))); | ||
+ | } | ||
+ | // else((c >= 97+(atoi(*(argv+1)))) && (c <= 122+(atoi(*(argv+1))))) | ||
+ | // { | ||
+ | // c = c - (atoi(*(argv+1))); | ||
+ | // } | ||
+ | if((c == (122+1+(atoi(*(argv+1))))) || (c == (90+1+(atoi(*(argv+1)))))) | ||
+ | { | ||
+ | c = c - (atoi(*(argv+1))); | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | c = fgetc(in); | ||
+ | } | ||
+ | |||
+ | |||
+ | fclose(in); | ||
+ | fclose(on); | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This took forever..... | ||
+ | =====Execution===== | ||
+ | |||
+ | An example run of the enciphering process: | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | your text is: ok so here is the final test code. zebras are cool and i have a brain. | ||
+ | |||
+ | the enciphered text is: rn vr khuh lv wkh ilqdo whvw frgh. }heudv duh frro dqg l kdyh d eudlq. | ||
+ | lab46: | ||
+ | |||
+ | </ | ||
+ | |||
+ | Now, we switch gears and decipher a different (previously enciphered) message: | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | enciphered text is: rn vr khuh lv wkh ilqdo whvw frgh. }heudv duh frro dqg l kdyh d eudlq. | ||
+ | |||
+ | actual txt is: ok so here is the final test code. zebras are cool and i have a brain. | ||
+ | lab46: | ||
+ | |||
+ | </ | ||
+ | =====Reflection===== | ||
+ | OH MY WORD... ok so first of all one of the funnest programs so far. it was so frustrating but so freak out rewarding when it worked. When dealing with the character z it was difficult but using the ASCII charts i found a way to fix the situation. This really tested me especially because I'm picky. Completing it was so epic though. This was a great project that pushed the boundaries of my mind to its limit only to realize it expands further looking forward to more programs as enjoyable as this one. | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | * Irc chat | ||
+ | * Matthew Haas with test1.c (THANKS) |