User Tools

Site Tools


user:brobbin4:portfolio:cprogproject1

Project: SECRET AGENT

A project for CSCS1320 C\C++ Programmint by Brian Robbins during the Spring 2012 Semester.

This project was begun on February 20th, 2012 and is anticipated to take 1 week to complete. Project was completed on February 25, 2012.

Objectives

The purpose of this project was to put to use all of the things we have learned in class thus far. When this project has reached completion I will have two programs. The first program will cipher a message contained within a text file with a cipher key found in another text file. This key is actually used to figure out the number of shifts that will occur durring the ciphering. The resultant of this program will be a text document that contains the ciphered message. The second program witt decipher the previously ciphered message using the same technique but in reverse essentially reversing the effects of the original ciphering program. The output from this deciphering program will be a text document with the exact same message in it as the original message file used in the original cipher program. By completing this project I hope to gain a better understanding of how if statements work, and how external files are utilized within a program. This will also help me understand basic mathmatical concepts within the c programming language.

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 idea for this project is to create a program or programs that will cipher and decipher a message contained within a text document.

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:

/*
 * 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: message.txt
 * Enciphered message placed in: cipher.txt
 *
 * Execute with: ./encipher    or    ./encipher KEYVAL
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(int argc, char **argv)
{
	FILE *key, *message, *cipher;
	int code;
	long long int c;
	char fname[]= "message.txt";
 
	key=fopen("key.txt","r+");
	message=fopen(fname,"r+");
	cipher=fopen("cipher.txt","w+");
 
	if(key == NULL)
	{
		printf("\n");
		printf("key.txt file does not exist. Please make sure a valid key.txt exists in the same folder as this program.");
		printf("\n");
		exit(1);
	}
 
	if(message == NULL)
	{
		printf("\n");
		printf("message.txt file does not exist. Please make sure plain.txt is present in the same directory as this program");
		printf("\n");
		exit(1);
	}
 
	printf("\n");
	printf("Enciphering process initiated.");
	printf("\n");
 
	if(argc == 2)
	{
        	printf("Enciphering was called with the following cipher key: %d\n", atoi(*(argv+1)));
		code = atoi(*(argv+1));
	}
 
	else
	{
        	printf("No argument was provided durring execution. Geting cipher key from 'key.txt'\n");
		fscanf(key, "%d", &code);
		printf("Cipher key is %d\n", code);
	}
 
	printf("Enciphering the message contained within 'message.txt'!");
	printf("\n");
 
        c = fgetc(message);
        while(c != EOF)
        {
 
                if((c >= 'A') && (c <= 'M'))
                {
			c = c + code;
			while(c > 77)
			{
				c = c - 13;
 
			}
                }
 
		else if((c >= 'N') && (c <= 'Z'))
		{
			c = c - code;
			while(c < 78)
			{
				c = c + 13;
			}
		}
 
		if((c >= 'a') && (c <= 'm'))
		{
			c = c + code;
			while(c > 109)
			{
				c = c - 13;
			}
		}
 
		else if((c >= 'n') && (c <= 'z'))
		{
			c = c - code;
			while(c < 110)
			{
				c = c + 13;
			}
		}
 
                fprintf(cipher, "%c", c);
		fprintf(stdout, "%c", c);
                c = fgetc(message);
        }
 
        fclose(message);
 
	printf("Enciphering complete! Enciphered message is stored in cipher.txt.");
	printf("\n");
 
	return(0);
}

The decipher code:

/*
 * decipher.c - program that decodes a message according to a key
 *
 *
 * Compile with: gcc -o decipher decipher.c
 *
 * Place key value in: key.txt
 * Place message to decipher in: cipher.txt
 * deciphered message placed in: plain.txt
 *
 * Execute with: ./decipher    or    ./decipher KEYVAL
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(int argc, char **argv)
{
	FILE *key, *message, *decipher;
	int code;
	long long int c;
	char fname[]= "cipher.txt";;
 
	key=fopen("key.txt","r+");
	message=fopen(fname,"r+");
	decipher=fopen("plain.txt","w+");
 
	if(key == NULL)
	{
		printf("\n");
		printf("key.txt file does not exist. Please make sure a valid key.txt exists in the same folder as this program.");
		printf("\n");
		exit(1);
	}
 
	if(message == NULL)
	{
		printf("\n");
		printf("cipher.txt file does not exist. Please make sure cipher.txt is present in the same directory as this program");
		printf("\n");
		exit(1);
	}
 
	printf("\n");
	printf("Deciphering process initiated.");
	printf("\n");
 
	if(argc == 2)
	{
        	printf("Deciphering was called with the following cipher key: %d\n", atoi(*(argv+1)));
		code = atoi(*(argv+1));
	}
 
	else
	{
        	printf("No argument was provided durring execution. Geting cipher key from 'key.txt'\n");
		fscanf(key, "%d", &code);
		printf("Cipher key is %d\n", code);
	}
 
	printf("Deciphering message contained within 'cipher.txt'!");
	printf("\n");
 
        c = fgetc(message);
        while(c != EOF)
        {
		if((c >= 'A') && (c <= 'M'))
                {
                        c = c - code;
			while(c < 65)
			{
				c = c + 13;
			}
                }
 
                else if((c >= 'N') && (c <= 'Z'))
                {
                        c = c + code;
			while(c > 90)
			{
				c = c - 13;
			}
                }
 
                if((c >= 'a') && (c <= 'm'))
                {
                        c = c - code;
			while(c < 97)
			{
				c = c + 13;
			}
                }
 
                else if((c >= 'n') && (c <= 'z'))
                {
                        c = c + code;
			while(c > 122)
			{
				c = c - 13;
			}
                }
 
                fprintf(decipher, "%c", c);
		fprintf(stdout, "%c", c);
                c = fgetc(message);
        }
 
        fclose(message);
 
	printf("Deciphering complete! Deciphered message is stored in plain.txt.");
	printf("\n");
 
	return(0);
}

Execution

An example run of the enciphering process:

lab46:~/src/cprog/cipher$ ./encipher

Enciphering process initiated.
No argument was provided durring execution. Geting cipher key from 'key.txt'
Cipher key is 18246975
Enciphering the message contained within 'message.txt'!
Nabz wyvckin qgz g yvsge wgbu nv lbmoyk von!
Enciphering complete! Enciphered message is stored in cipher.txt.
lab46:~/src/cprog/cipher$

An example run of the deciphering process:

lab46:~/src/cprog/cipher$ ./decipher

Deciphering process initiated.
No argument was provided durring execution. Geting cipher key from 'key.txt'
Cipher key is 18246975
Deciphering message contained within 'cipher.txt'!
This project was a royal pain to figure out!
Deciphering complete! Deciphered message is stored in plain.txt.
lab46:~/src/cprog/cipher$

Reflection

This project was alot tougher than originally thought. I thought the shift thing would be really easy but it actually took research and colaboration from other class mates to be able to complete this project. I now completely understand 'if' statements and how to call enternal files from within a program.

References

In performing this project, the following resources were referenced:

  • Lab46 Class chat
  • Matt's hint
  • C Pocket Reference
  • Fellow classmates
user/brobbin4/portfolio/cprogproject1.txt · Last modified: 2012/02/29 10:38 by brobbin4