======Project: SECRET AGENT======
A project for C/C++ Programming by Josh Cavaluzzi during the Spring 2012.
This project was begun on February 23, 2012 and is anticipated to (hopefully) take one day to complete. Project was completed on April 3, 2012.
=====Objectives=====
The purpose of this project is to use what the class has learned so far, such as file manipulation, arrays, pointers, addresses, etc., and create a program that will take any statement or message, store it into a file, and encode it into another file. Then, another program will be made to decipher the code using the same key that was used to encode the message.
=====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 this project is to use what I have learned so far, such as pointer arrays, manipulation of files, calling from and printing to files, and calling from arguments, and create a program that will encipher a code that is given or taken from a file. Also, I must create a program that will decipher whatever is desired.
The encipher program will begin with a user starting the program and giving it a number argument that will be used as the key, if not, the program will use a specified key that the user can find in a file. The user will be shown what the message was, or what the user input if the message is in a file, and then the program will mess with the message and manipulate it, printing it back out onto the screen and into a new file.
The decipher code will do the same thing, except it will change the code back into a readable 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:
/*
* This is the cypher encoding program for lab #1.
*
* 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 or ./enciphber KEYVAL
*
*
*/
#include
#include
int main(int argc, char **argv)
{
FILE *in, *out, *encipherkey;
char letter, *message, i, result;
int key;
encipherkey=fopen("key.txt", "r");
in=fopen("plain.txt", "r");
out=fopen("message.txt", "w");
if(argc == 2)
{
printf("This is what you want the key to be: %d\n", atoi(*(argv + 1)));
key = atoi(*(argv + 1));
}
else
{
fscanf(encipherkey, "%hdd", &key);
printf("This is the key for the encipher: %d\n", key);
}
/*
*
* This if block will check the plain.txt file to see if it has a message
* and if it doesn't, it will ask the user to input a message to encipher.
* SOMETHING IS GOING WRONG DOWN THERE! FIX IT SOON!
*/
if(in == NULL)
{
printf("Please enter a secretive secret message\n");
message = (char *) malloc(sizeof(unsigned char) * 100);
fgets(message, 99, stdin);
printf("This is the message: ");
i = 0;
while(*(message + i) != '\0')
{
printf("%c", *(message + i));
i++;
}
printf("\nThis is the cipher: ");
i = 0;
while(*(message + i) != '\n')
{
if((*(message + i) >= 'A') && (*(message + i) <= 'Z'))
result = ((((*(message + i) - 64) + key) % 26) + 64);
else if((*(message + i) >= 'a') && (*(message + i) <= 'z'))
result = ((((*(message + i) - 96) + key) % 26) + 96);
else
result = *(message + i);
if( result == 64 || result == 96 )
result = result + 26;
i++;
fprintf(out, "%c", result);
printf("%c", result);
}
} /* End if block */
else
{
/*
*
* This while loop will take the message, character by character, and
* translate this into tdirectoryhe enciphered code using the given cipher key.
*
*/
while((letter = fgetc(in)) != EOF)
{
if((letter >= 'A') && (letter <= 'Z'))
result = ((((letter - 64) + key) % 26) + 64);
else if((letter >= 'a') && (letter <= 'z'))
result = ((((letter - 96) + key) % 26) + 96);
else
result = letter;
if( result == 64 || result == 96 )
result = result + 26;
fprintf(out, "%c", result);
printf("%c", result);
}
fclose(in);
fclose(out);
fclose(encipherkey);
}
printf("\n\n");
return(0);
}
Next, I will show you the decipher code. Brilliant!
/*
*
* This is the decipher program, it will take an already enciphered message
* and change it back into the original message using the key given.
*
*
*/
#include
#include
int main(int argc, char **argv)
{
FILE *in, *out, *encipherkey;
char letter, *message, i, result, pfile;
int key;
encipherkey=fopen("key.txt", "r");
in=fopen("cipher.txt", "r");
out=fopen("original.txt", "w");
if(argc == 2)
{
printf("This is what you want the key to be: %d\n", atoi(*(argv + 1)));
key = atoi(*(argv + 1));
}
else
{
fscanf(encipherkey, "%hdd", &key);
printf("This is the key for the encipher: %d\n", key);
}
if(in == NULL)
{
printf("Please enter enciphered secretive secret message\n");
message = (char *) malloc(sizeof(unsigned char) * 100);
fgets(message, 99, stdin);
fgets(message, 99, stdin);
printf("This is the enciphered message: ");
i = 0;
while(*(message + i) != '\0')
{
printf("%c", *(message + i));
i++;
}
printf("\nThis is the deciphered message: ");
i = 0;
while(*(message + i) != '\n')
{
if((*(message + i) >= 'A') && (*(message + i) <= 'Z'))
{
result = ( ( *( message + i ) - 64 ) - key );
if( result < 0 )
{
result = result + 26;
result = ( ( result % 26 ) + 64 );
}
else
result = ( ( result % 26 ) + 64 );
}
else if((*(message + i) >= 'a') && (*(message + i) <= 'z'))
{
result = ( ( *( message + i ) - 96 ) - key );
if( result < 0 )
{
result = result + 26;
result = ( ( result % 26 ) + 96 );
}
else
result = ( ( result % 26 ) + 96 );
}
else
result = *(message + i);
if( result == 64 || result == 96 )
result = result + 26;
i++;
printf("%c", result);
fprintf(out, "%c", result);
}
} /* End if block */
else
{
/*
*
* This while loop will take the message, character by character, and
* translate this into tdirectoryhe enciphered code using the given cipher$
*
*/
printf("\nThis is the message: ");
while((pfile = fgetc(in)) != EOF)
{
printf("%c", pfile);
i++;
}
fclose(in);
in=fopen("cipher.txt", "r");
printf("\nThis is the cipher: ");
while((letter = fgetc(in)) != EOF)
{
if((letter >= 'A') && (letter <= 'Z'))
{
result = ( ( letter - 64 ) - key );
if( result < 0 )
{
result = result + 26;
result = ( ( result % 26 ) + 64 );
}
else
{
result = ( ( result % 26 ) + 64 );
}
}
else if((letter >= 'a') && (letter <= 'z'))
{
result = ( ( letter - 96 ) - key );
if( result < 0 )
{
result = result + 26;
result = ( ( result % 26 ) + 96 );
}
else
{
result = ( ( result % 26 ) + 96 );
}
}
else
result = letter;
if( result == 64 || result == 96 )
result = result + 26;
fprintf(out, "%c", result);
printf("%c", result);
}
printf("You can find the new decoded message by opening the file 'original.txt'.");
fclose(in);
fclose(out);
fclose(encipherkey);
}
printf("\n\n");
return(0);
}
=====Execution=====
An example run of the enciphering process:
lab46:~/src/cprog/Projects/project1$ ./encipher
This is the key for the encipher: 12
This is the message: Hello. This is Josh Cavaluzzi. The cipher has been complete. Here is a test.
This is the cipher: Tqxxa. Ftue ue Vaet Omhmxgllu. Ftq oubtqd tme nqqz oaybxqfq. Tqdq ue m fqef.
You can find the new encoded message by opening the file 'cipher.txt'.
lab46:~/src/cprog/Projects/project1$ cat plain.txt
Hello. This is Josh Cavaluzzi. The cipher has been complete. Here is a test.
lab46:~/src/cprog/Projects/project1$ cat cipher.txt
Tqxxa. Ftue ue Vaet Omhmxgllu. Ftq oubtqd tme nqqz oaybxqfq. Tqdq ue m fqef.
lab46:~/src/cprog/Projects/project1$
Now, we switch gears and decipher a different (previously enciphered) message:
lab46:~/src/cprog/Projects/project1$ ./decipher
This is the key for the encipher: 12
This is the message: Tqxxa. Ftue ue Vaet Omhmxgllu. Ftq oubtqd tme nqqz oaybxqfq. Tqdq ue m fqef.
This is the cipher: Hello. This is Josh Cavaluzzi. The cipher has been complete. Here is a test.
You can find the new decoded message by opening the file 'original.txt'.
lab46:~/src/cprog/Projects/project1$ cat cipher.txt
Tqxxa. Ftue ue Vaet Omhmxgllu. Ftq oubtqd tme nqqz oaybxqfq. Tqdq ue m fqef.
lab46:~/src/cprog/Projects/project1$ cat original.txt
Hello. This is Josh Cavaluzzi. The cipher has been complete. Here is a test.
lab46:~/src/cprog/Projects/project1$
=====Reflection=====
Wow. That project took much longer than I had anticipated. I am glad that I have finally finished it. I did enjoy fixing it, I like going through broken code and making it work, even when I have to ask questions. This project was fun, and I hope that I can complete the next project with more ease.
=====References=====
In performing this project, the following resources were referenced:
* http://www.asciitable.com/ ( Allowed me to quickly reference the ASCII table and find out what parameters I needed to set )
Generally, state where you got informative and useful information to help you accomplish this project when you originally worked on it (from Google, other wiki documents on the Lab46 wiki, etc.)