User Tools

Site Tools


user:swilli31:portfolio:cprogproject1

Project: SECRET AGENT

A project for C/C++ Programming by Stephanie Williams during the Spring Semester 2012.

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

Objectives

The purpose of this project is to experiment with arrays, pointers, and use pointer arithmetic. This project is to test the abilities of the programmer to check understanding of lessons taught thus far.

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

This project is to explore the world of C by writing a code that can make a phrase unreadable to the human eye. It is then supposed to decode that same message that was encoded. This type of spy craft is used throughout the world of espionage and this is just the tip of the iceberg.

C is a versatile language that has been used for many years and has been the foundation for many programs used in the world of computing today.

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

/*
 *This program is to encipher
 * a message and save it to
 *a file
 *
 *execute with ./encrypt
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        FILE *in, *cipher;
        char c, fname[] = "plain.txt";
        char code[] = "cipher.txt";
 
        in = fopen(fname, "r");
        cipher = fopen(code, "w");
 
        if(in == NULL)
        {
                fprintf(stderr, "ERROR!.\n");
                fprintf(stderr, "No Messege found.\n");
                exit(1);
        }
 
        c = fgetc(in);
        while(c != EOF)
        {
                if((c >= 65) && (c <= 'Z'))
                        c++;
                else if((c >= 'a') && (c <= 'z'))
                        c++;
 
                if((c == ('Z' + 1)) || (c == ('z' + 1)))
                        c = c - 26;
 
                fprintf(stdout, "%c", c);
                fprintf(cipher, "%c", c);
                c = fgetc(in);
        }
        fclose(in);
        fclose(cipher);
        return(0);
}

Execution

An example run:

lab46:~/src/cprog$ ./secret_agent
Upebz jt b hppe ebz gps njtijfg boe nbzifn.
lab46:~/src/cprog$ vi messege.txt
lab46:~/src/cprog$ cat messege.txt
oday is a good day for mishief and mayhem.
ÿ
lab46:~/src/cprog$ cat cipher.txt
Upebz jt b hppe ebz gps njtijfg boe nbzifn.

Reflection

The original message was almost copied perfectly except for the first letter 'T'. Upon completion of this project, I realize I need to play more with the C programming language to be more comfortable with it.

References

In performing this project, the following resources were referenced:

  • class notes
  • book
user/swilli31/portfolio/cprogproject1.txt · Last modified: 2012/02/26 02:43 by swilli31