User Tools

Site Tools


user:sswimle1:portfolio:cprogproject1

Project: SECRET AGENT

A project for C/C++ Programing by Shane Swimley during the Spring 2012.

This project was begun on 2/24/12 and is anticipated to take 1 day to complete. Project was completed on MONTH DAY, YEAR.

Objectives

State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it?

Write a program that will encipher and decipher a given string of text.

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

State the idea or purpose of the project. What are you attempting to pursue?

Making a program that will take a message, encipher the message and print the encipher message out to the screen and a location in memory. Then taking the enciphered message, and deciphering it back into the readable message, displaying that to the screen and in a location of memory.

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<stdio.h>
#include<stdlib.h>
 
int main()
{
        FILE *message, *key, *cipher;
        char c, fname[] = "message.txt";
        char code[] = "key.txt";
        int keyvalue;
 
        message = fopen(fname, "r");
        key = fopen(code, "r");
        cipher = fopen("cipher.txt", "w");
 
        c = fgetc(message);
        fscanf(key,"%d",&keyvalue);
 
        printf("Message is: ");
 
        while(c != EOF)
        {
                fprintf(stdout, "%c", c);
                c = fgetc(message);
        }
 
        fclose(message);
        message = fopen(fname, "r");
        c = fgetc(message);
 
        printf("Cipher is: ");
 
        while(c != EOF)
        {
                if((c >= 65) && (c <= 'Z'))
                        c = c + keyvalue;
                else if((c >= 'a') && (c <= 'z'))
                        c = c + keyvalue;
 
                fprintf(stdout, "%c", c);
                fprintf(cipher, "%c", c);
                c = fgetc(message);
        }
        fclose(message);
        fclose(key);
        return(0);
}

Don't forget your deciphering code as well.

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
        FILE *cipher, *key, *decipher;
        char c, fname[] = "cipher.txt";
        char code[] = "key.txt";
        int keyvalue;
 
        cipher = fopen(fname, "r");
        key = fopen(code, "r");
        decipher = fopen("decipher.txt", "w");
 
        c = fgetc(cipher);
        fscanf(key,"%d",&keyvalue);
 
        printf("Cipher is: ");
 
        while(c != EOF)
        {
                fprintf(stdout, "%c", c);
                c = fgetc(cipher);
        }
 
        fclose(cipher);
        cipher = fopen(fname, "r");
        c = fgetc(cipher);
 
        printf("Decipher is: ");
 
        while(c != EOF)
        {
                if((c >= 65) && (c <= 'Z'))
                        c = c - keyvalue;
                else if((c >= 'a') && (c <= 'z'))
                        c = c - keyvalue;
 
                fprintf(stdout, "%c", c);
                fprintf(decipher, "%c", c);
                c = fgetc(cipher);
        }
        fclose(cipher)
        fclose(key);
        return(0);
}

Execution

An example run of the enciphering process:

lab46:~/src/cprog$ ./project2
Message is: deadbeef
Cipher is: fgcfdggh
lab46:~/src/cprog

Now, we switch gears and decipher a different (previously enciphered) message:

lab46:~/src/cprog$ gcc -o project2.1 project2.1.c
lab46:~/src/cprog$ ./project2.1
Cipher is: fgcfdggh
Decipher is: deadbeef

Reflection

Comments/thoughts generated through performing the project, observations made, analysis rendered, conclusions wrought. What did you learn from doing this project?

I do not think that the overall concept behind the code is hard part of a project like this for me, I think starting something like this from scratch after having about 5 weeks of C/C++ face time is the difficult part. Once I was introduced to concepts for completing the task I found that with little to no guidence I could write the decipher program on my own, starting the cipher program was the difficult part.

Now that I have a working example I can take some more time to look at the code and improve my concept of creating something like this from scratch.

References

In performing this project, the following resources were referenced:

Matt's example posted.

Other students code.

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.)

user/sswimle1/portfolio/cprogproject1.txt · Last modified: 2012/02/28 18:28 by sswimle1