====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:
/*
* 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("Greetings, Agent Haas.\n");
FILE *in, *out;
int cipher;
int key;
in = fopen("plain.txt", "r");
out = fopen("enciphered.txt", "w");
if(argv[1] == NULL)
{
printf("\nNo argument provided, execute with key argument.\nExample: ./encipher 1\n");
exit(1);
}
char *plain;
plain = (char*) malloc (sizeof(char) * 127);
int count = 0;
if(in == NULL)
{
printf("\nFile empty, enter some text to encipher\n");
fgets(plain, 127, stdin);
while(plain[count] != '\0')
{
count = count+1;
}
int cipher = atoi(argv[1]);
count = 0;
while(plain[count] != '\0')
{
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;
while(key > 122)
{
key = key - 26;
}
}
if(key == 1)
{
key = 32;
}
fprintf(out, "%c", key);
count = count+1;
}
printf("\nEnciphered text is loated in enciphered.txt\n");
}
else
{
fgets(plain, 127, in);
printf("\nText before cipherication: ");
while(plain[count] != '\0')
{
printf("%c", plain[count]);
count = count+1;
}
int cipher = atoi(argv[1]);
count = 0;
printf("\nCiphered text output: ");
while(plain[count] != '\0')
{
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;
while(key > 122)
{
key = key - 26;
}
}
if(key == 1)
{
key = 32;
}
printf("%c", key);
fprintf(out, "%c", key);
count = count+1;
}
printf("\nEnciphered text is located in enciphered.txt\n\n");
fclose(in);
fclose(out);
return (0);
}
}
Decipher:
/*
* 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("enciphered.txt", "r");
out = fopen("plain.txt", "w");
if(argv[1] == NULL)
{
printf("Execute with key argument.\nExample: ./decipher 1\n");
exit(1);
}
char *enciphered;
enciphered = (char*)malloc(sizeof(char) * 127);
int count = 0;
fgets(enciphered, 127, in);
printf("\nEnciphered text being deciphered: ");
while(*(enciphered+count) != '\0')
{
printf("%c", enciphered[count]);
count = count+1;
}
int cipher = atoi(argv[1]);
count = 0;
printf("\nDeciphered text: ");
while(enciphered[count] != '\0')
{
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("%c", key);
fprintf(out, "%c", key);
count = count+1;
}
printf("\nDeciphered text wirtten to plain.txt\n\n");
fclose(in);
fclose(out);
return (0);
}
lab46:~/src/cprog/cipher$ ./encipher 1
Greetings, Agent Haas.
Text before cipherication: Greeting Haas, reports suggest the Ocean is big. That is all.
Ciphered text output: Hsffujoh Ibbt sfqpsut tvhhftu uif Pdfbo jt cjh Uibu jt bmm
Enciphered text is located in enciphered.txt
lab46:~/src/cprog/cipher$ ./decipher 1
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:~/src/cprog/cipher$
====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://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.2.html
- http://www.asciitable.com/
- http://cboard.cprogramming.com/c-programming/109142-caesar-cipher.html