======Project: SECRET AGENT====== A project for C++ by Robert Matsch during the Spring 2012. This project was begun on febuary 23 and is anticipated to take 2 days to complete. Project was completed on 2/25/2012 =====Objectives===== The purpose of this project is to encode and decode a message using a program. =====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===== The purpose of the project is to take a message and encipher it with a key. The enciphered message should be sent to a file which you can read. you should also have program that thakes and encipher message and can decipher the message to come up with the original message. =====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: #include #include int main(){ char letter1=0; char msg[250]; FILE *in, *out, *key,*nf; char letter = 0; int keyvalue; nf = fopen("nofile.txt", "w"); in = fopen("message.txt", "r"); out = fopen("cipher.txt", "w"); key = fopen("key.txt","r"); if (in == NULL){ fputs("please enter a message to cipher:\n", stdout); fgets(msg, sizeof msg, stdin); fprintf(nf, "%s", msg); fclose(nf); nf = fopen("nofile.txt", "r"); letter1 = fscanf(nf,"%c", &letter); }else{ letter1 = fscanf(in, "%c", &letter); }if (key == NULL){ printf("please enter a key to encipher with :\n"); scanf("%d", &keyvalue); }else{ fscanf(key,"%d",&keyvalue); } while (letter1 != EOF) { if((letter >= 'A') && (letter <= 'Z')) letter = (((abs(letter - 65) + keyvalue)%26)+65); else if((letter >= 'a') && (letter <= 'z')) letter = (((abs(letter - 97) + keyvalue)%26)+97); fprintf(out,"%c",letter); if (in == NULL){ letter1 = fscanf(nf, "%c", &letter); }else{ letter1 = fscanf(in, "%c", &letter); } }if (in == NULL){ fclose(nf); }else{ fclose(in); } if (key != NULL){ fclose(key); } fclose(out); return(0); } Decipher code. #include #include #include int main(){ char letter1=0; char msg[250]; FILE *in, *out, *key,*nf, *cipher; char letter = 0; int keyvalue; cipher = fopen("cipher.txt", "r"); out = fopen("plain.txt", "w"); letter1 = fscanf(cipher, "%c",&letter); printf("the message to decipher is %c\n", letter); printf("please enter a key to decipher\n"); fscanf(stdin, "%d", &keyvalue); while (letter1 != EOF) { if((letter >= 'A') && (letter <= 'Z')) letter = (((abs(letter - 65) - keyvalue)%26)+65); else if((letter >= 'a') && (letter <= 'z')) { letter = ( letter-97)-keyvalue; if (letter<0){ letter =(26+letter); } letter = letter +97; } fprintf(stdout, "%c", letter); fprintf(out,"%c",letter); letter1 = fscanf(cipher, "%c",&letter); } fclose(cipher); fclose(out); return(0); } =====Execution===== An example run of the enciphering process: lab46:~/src/cprog$ ./prog1x4 please enter a message to cipher: hey please enter a key to encipher with : 2 lab46:~/src/cprog$ lab46:~/src/cprog$ cat cipher.txt jga lab46:~/src/cprog$ Now, we switch gears and decipher a different the message: lab46:~/src/cprog$ echo "12" > key.txt lab46:~/src/cprog$ echo "jga" > cipher.txt lab46:~/src/cprog$ ./prog1x4 Cipher key found in key.txt: 12 Cipher is: hja Message is: hey lab46:~/src/cprog$ cat plain.txt hey lab46:~/src/cprog$ =====Reflection===== upon completion of project 1 i have learned a easy way to encipher and decipher and message. =====References===== In performing this project, the following resources were referenced: * C ++ (the C anci)