User Tools

Site Tools


user:jpettie:portfolio:cprogproject1

Project: SECRET AGENT

A project for C/C++ Programming by Jacob Pettie during the Spring 2012 Semester.

Objectives

The purpose of this project is to create a working program that will encipher and decipher a given input using files and pointers.

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 encipher and decipher a message by shifting the alphabet by a key value inputted by a user.

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/decipher code:

/*
 * cipher.c - program that encodes and decodes a message according to a key
 *
 *
 * Compile with: gcc -o cipher cipher.c
 *
 * Place key value in command line argument
 * Place message to cipher in: cipher.txt
 * Ciphered message placed in: decipher.txt
 *
 * Execute with: ./cipher TYPE(c or d) KEYVAL(num)
 */
 
#include <stdio.h>
#include <math.h>
 
void cipher(char);
void decipher(char);
 
int main(int argc, char **argv){
        if(argc == 3){
                if (argv[1][0] == 'c'){
                        cipher(atoi(argv[2]));
                }else if (argv[1][0] == 'd'){
                        decipher(atoi(argv[2]));
                }else{
                        printf("Incorrect first argument.\n");
                }
        }else{
                printf("Not enough arguments were provided.\n");
        }
        return(0);
}
 
void cipher(char num){
        char cKey;
        FILE *cipher, *decipher;
        cipher = fopen("cipher.txt", "r");
        if (cipher == NULL){
                printf("File does not exist.\n");
                return;
        }
        cKey = fgetc(cipher);
        if (cKey == EOF){
                printf("There is nothing in the file.");
                scanf("Please enter a message to cipher.\n", &cKey);
        }else{
                fclose(cipher);
                cipher = fopen("cipher.txt", "r");
                cKey = fgetc(cipher);
        }
        while (cKey != EOF){
                printf("%c", cKey);
                cKey = fgetc(cipher);
        }
        fclose(cipher);
        cipher = fopen("cipher.txt", "r");
        decipher = fopen("decipher.txt", "w");
        cKey = fgetc(cipher);
        while (cKey != EOF){
                if (cKey >= 'A' && cKey <= 'Z'){
                        cKey = (((cKey - 65) + num)%26) + 65;
                }else if (cKey >= 'a' && cKey <= 'z'){
                        cKey = (((cKey - 97) + num)%26) + 97;
                }
                printf("%c", cKey);
                fprintf(decipher, "%c", cKey);
                cKey = fgetc(cipher);
        }
        fclose(cipher);
        fclose(decipher);
}
 
void decipher(char num){
        char cKey;
        FILE *decipher;
        decipher = fopen("decipher.txt", "r");
        if (decipher == NULL){
                printf("There is no message to decipher.");
                return;
        }
        cKey = fgetc(decipher);
        while (cKey != EOF){
                printf("%c", cKey);
                cKey = fgetc(decipher);
        }
        fclose(decipher);
        decipher = fopen("decipher.txt", "r");
        cKey = fgetc(decipher);
        while (cKey != EOF){
                if (cKey >= 'A' && cKey <= 'Z'){
                        cKey = ((abs((cKey - 65) - num))%26) + 65;
                }else if (cKey >= 'a' && cKey <= 'z'){
                        cKey = ((abs((cKey - 97) - num))%26) + 97;
                }
                printf("%c", cKey);
                cKey = fgetc(decipher);
        }
        fclose(decipher);
}

Execution

Running the program to encipher an input:

jpettie@lab46:~/src/cprog$ ./cipher c 30
This is the sentence to play with
Xlmw mw xli wirxirgi xs tpec amxl
jpettie@lab46:~/src/cprog$

Running the same program to decipher an input:

jpettie@lab46:~/src/cprog$ ./cipher d 30
Xlmw mw xli wirxirgi xs tpec amxl
Htsi si htw iwnhwnyw hm lpac esht
jpettie@lab46:~/src/cprog$

Reflection

In reflection, I would instead of waiting until the last minute to complete the project, I will in the future give myself more time to complete the project. This will allow for the experienced problems with the alphabet to be weeded out before it is too late to have time to address such problems.

References

I used the following references to complete this project:

user/jpettie/portfolio/cprogproject1.txt · Last modified: 2012/02/28 16:37 by jpettie