This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:asowers:secret_agent [2012/09/22 18:07] – [Code] asowers | user:asowers:secret_agent [2012/09/24 17:08] (current) – [Code] asowers | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====Welcome Agent==== | ||
+ | ====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 “plain.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==== | ||
+ | Encipher: | ||
+ | <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: plain.txt | ||
+ | * Enciphered message placed in: cipher.txt | ||
+ | * | ||
+ | * Execute with: ./encipher KEYVAL | ||
+ | * | ||
+ | */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | |||
+ | printf(" | ||
+ | FILE *in, *out; | ||
+ | |||
+ | int cipher; | ||
+ | |||
+ | int key; | ||
+ | |||
+ | in = fopen(" | ||
+ | out = fopen(" | ||
+ | |||
+ | if(argv[1] == NULL) | ||
+ | |||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | char *plain; | ||
+ | plain = (char*) malloc (sizeof(char) * 127); | ||
+ | int count = 0; | ||
+ | |||
+ | if(in == NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | fgets(plain, | ||
+ | |||
+ | while(plain[count] != ' | ||
+ | { | ||
+ | |||
+ | count = count+1; | ||
+ | } | ||
+ | |||
+ | int cipher = atoi(argv[1]); | ||
+ | count = 0; | ||
+ | |||
+ | while(plain[count] != ' | ||
+ | { | ||
+ | key = 1; | ||
+ | |||
+ | if(plain[count] >= 65 && plain[count] <= 90) | ||
+ | { | ||
+ | key = plain[count] + cipher; | ||
+ | |||
+ | while(key > 90) | ||
+ | { | ||
+ | key = key - 26; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if(plain[count] >= 97 && plain[count] <= 122) | ||
+ | { | ||
+ | key = plain[count] + cipher; | ||
+ | |||
+ | | ||
+ | { | ||
+ | key = key - 26; | ||
+ | } | ||
+ | } | ||
+ | if(key == 1) | ||
+ | { | ||
+ | key = 32; | ||
+ | } | ||
+ | |||
+ | fprintf(out, | ||
+ | count = count+1; | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | else | ||
+ | { | ||
+ | fgets(plain, | ||
+ | printf(" | ||
+ | while(plain[count] != ' | ||
+ | { | ||
+ | printf(" | ||
+ | count = count+1; | ||
+ | } | ||
+ | int cipher = atoi(argv[1]); | ||
+ | count = 0; | ||
+ | printf(" | ||
+ | while(plain[count] != ' | ||
+ | { | ||
+ | key = 1; | ||
+ | if(plain[count] >= 65 && plain[count] <= 90) | ||
+ | { | ||
+ | key = plain[count] + cipher; | ||
+ | while(key > 90) | ||
+ | { | ||
+ | key = key - 26; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | { | ||
+ | key = plain[count] + cipher; | ||
+ | while(key > 122) | ||
+ | { | ||
+ | key = key - 26; | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | key = 32; | ||
+ | } | ||
+ | printf(" | ||
+ | fprintf(out, | ||
+ | count = count+1; | ||
+ | } | ||
+ | printf(" | ||
+ | fclose(in); | ||
+ | fclose(out); | ||
+ | |||
+ | return (0); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | Decipher: | ||
+ | <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: plain.txt | ||
+ | * Enciphered message placed in: cipher.txt | ||
+ | * | ||
+ | * Execute with: ./decipher KEYVAL | ||
+ | * | ||
+ | */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | |||
+ | FILE *in, *out; | ||
+ | int key; | ||
+ | in = fopen(" | ||
+ | out = fopen(" | ||
+ | if(argv[1] == NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | char *enciphered; | ||
+ | enciphered = (char*)malloc(sizeof(char) * 127); | ||
+ | int count = 0; | ||
+ | |||
+ | fgets(enciphered, | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | while(*(enciphered+count) != ' | ||
+ | { | ||
+ | printf(" | ||
+ | count = count+1; | ||
+ | } | ||
+ | |||
+ | int cipher = atoi(argv[1]); | ||
+ | count = 0; | ||
+ | printf(" | ||
+ | while(enciphered[count] != ' | ||
+ | { | ||
+ | key = 1; | ||
+ | if(enciphered[count] >= 65 && enciphered[count] <= 90) | ||
+ | { | ||
+ | key = enciphered[count] - cipher; | ||
+ | |||
+ | while(key < 65) | ||
+ | { | ||
+ | key = key + 26; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if(enciphered[count] >= 97 && enciphered[count] <= 122) | ||
+ | { | ||
+ | key = enciphered[count] - cipher; | ||
+ | while(key < 97) | ||
+ | { | ||
+ | key = key + 26; | ||
+ | } | ||
+ | } | ||
+ | if(key == 1) | ||
+ | { | ||
+ | key = 32; | ||
+ | } | ||
+ | printf(" | ||
+ | fprintf(out, | ||
+ | count = count+1; | ||
+ | } | ||
+ | printf(" | ||
+ | |||
+ | fclose(in); | ||
+ | fclose(out); | ||
+ | |||
+ | return (0); | ||
+ | } | ||
+ | </ | ||
+ | <cli> | ||
+ | lab46: | ||
+ | Greetings, Agent Haas. | ||
+ | |||
+ | Text before cipherication: | ||
+ | |||
+ | Ciphered text output: Hsffujoh Ibbt sfqpsut tvhhftu uif Pdfbo jt cjh Uibu jt bmm | ||
+ | Enciphered text is located in enciphered.txt | ||
+ | |||
+ | lab46: | ||
+ | |||
+ | Enciphered text being deciphered: Hsffujoh Ibbt sfqpsut tvhhftu uif Pdfbo jt cjh Uibu jt bmm | ||
+ | Deciphered text: Greeting Haas reports suggest the Ocean is big That is all | ||
+ | Deciphered text wirtten to plain.txt | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | ====After Thoughts==== | ||
+ | This was a big jump from the previous project. This project properly demonstrated how to loop functions and preform mathematical functions while working with file I/O. | ||
+ | |||
+ | ====Sources==== | ||
+ | Studying previous student examples proved imparitive in association with the following websites: | ||
+ | - http:// | ||
+ | - http:// | ||
+ | - http:// |