User Tools

Site Tools


user:jdavis34:portfolio:cprogproject1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
user:jdavis34:portfolio:cprogproject1 [2012/02/25 19:28] – [Code] jdavis34user:jdavis34:portfolio:cprogproject1 [2012/02/28 19:57] (current) – [Code] jdavis34
Line 1: Line 1:
 +======Project: SECRET AGENT======
  
 +A project for C/C++ by Josh Davis during the Spring 2012.
 +
 +This project was begun on 2/14/2012 and is anticipated to take 2.5 Decades to complete. Project was completed on MONTH DAY, YEAR.
 +
 +=====Objectives=====
 +To write a program that depending on what it is asked to do will either cipher or decipher a 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
 +
 +
 +Thought the above listed are what are required... My unorthodox approach to completing stuff seemed to lead me in a direction that was slight variations of this.
 +
 +=====Background=====
 +Green with a blue border?
 +=====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).
 +
 +**Note: since the odd way I wrote my Cipher it changed the way that worked in relation to this, but the decipher is that of which was asked.**
 +
 +=====Code=====
 +
 +The encipher code:
 +Original cipher (Not the one that was meant to be wrote)
 +<code c>
 +  1 /*
 +  2  * encipher.c - program that encodes a message according to a key
 +  3  *
 +  4  *
 +  5  * Compile with: gcc -o encipher encipher.c
 +  6  *
 +  7  * Place key value in: key.txt
 +  8  * Place message to encipher in: plain.txt
 +  9  * Enciphered message placed in: cipher.txt
 + 10  *
 + 11  * Execute with: ./encipher    or    ./encipher KEYVAL
 + 12  *
 + 13  */
 + 14
 + 15 #include <stdio.h>
 + 16 #include <stdlib.h>
 + 17 int main(int argc, char **argv)
 + 18 {
 + 19         // File Info for input and output
 + 20         FILE *out,*out1;
 + 21 //        char value = 0;
 + 22 //        in=fopen("message.txt", "r");
 + 23 //      fscanf(in, "%c", &value);
 + 24         out=fopen("cipher.txt", "w");
 + 25         out1=fopen("key.txt","w");
 + 26 //        char *input, *pick;
 + 27 //        input=(char *)malloc(sizeof(char)*1);
 + 28 //        pick=(char *)malloc(sizeof(char)*1);
 + 29 //        *input=0;
 + 30 //        *pick=0;
 + 31         int key =0;
 + 32         unsigned char i;
 + 33         unsigned char q;
 + 34         char len=0,x,pos=0;
 + 35 //      printf("your program is broke\n");
 + 36         if(argc<2)
 + 37         {
 + 38                 printf("%8s must be run with 1 or\
 + 39                 more arguements, you only provided %hhu\n",*(argv+0),(argc-1));
 + 40                 exit(1);
 + 41         }
 + 42         printf("please enter a value in which you feel safe:\n");
 + 43         scanf("%d",&key);
 + 44         fprintf(out1, "%d",key);
 + 45 //        printf("you ran this program with %hhu arguments, they are:\n", argc);
 + 46         printf("your secret is safe with me:\n");
 + 47
 + 48         for(i=1; i<argc; i++)
 + 49         {
 + 50                 pos=0;
 + 51                 len=0;
 + 52 //              printf("testa\n");
 + 53                 x=*(*(argv+i)+pos);
 + 54                 fprintf(out, " ");
 + 55                 while(x!='\0')
 + 56                 {
 + 57                         len++;
 + 58                         pos++;
 + 59                         printf("%c", x+1);
 + 60                         fprintf(out,"%c", x+key);
 + 61                         x=*(*(argv+i)+pos);
 + 62                 }
 + 63 //              printf("\nthis ones for matt\n");
 + 64         //      for(pos=0;pos!=len+1;pos++)
 + 65         //      {
 + 66         //              printf("test b\n");
 + 67         //              printf("%c", *(*(argv+pos))+1);
 + 68         //      }
 + 69
 + 70                 printf(" ");
 + 71 //              for(q=0; q<argc; q++)
 + 72 //              {
 + 73 //
 + 74 //                      printf("%c",*(*(argv+i)+q)+1);
 + 75 //              }
 + 76 //              printf("\n");
 + 77         }
 + 78 //      fprintf(out,"\n -1");
 + 79 //      fclose(in);
 + 80         fclose(out);
 + 81         return(0);
 + 82 }
 + 83
 +                                                    
 +</code>
 +**NEW CIPHER**
 +<code>
 +  1 #include<stdio.h>
 +  2 #include<stdlib.h>
 +  3
 +  4 int main()
 +  5 {
 +  6         FILE *in,*out,*key;
 +  7         char c, fname[] = "cipher.txt";
 +  8 //      value = (char*)malloc(sizeof(char) * 250);
 +  9         char keyx =0;
 + 10 //      char x =0;
 + 11         key=fopen("key.txt", "r");
 + 12         in=fopen("plain.txt", "r");
 + 13         out=fopen("cipher.txt", "w");
 + 14         if (in == NULL)
 + 15         {
 + 16                 printf("ERROR!\n");
 + 17                 exit(1);
 + 18         }
 + 19 //        fgets(value, 250, in);
 + 20         fscanf(key, "%hhd", &keyx);
 + 21 //        while(*value != EOF)
 + 22 //      {
 + 23 //                printf("original values are %hhd,  ", value);
 + 24 //                x=(value-keyx);
 + 25 //                fprintf(out,"%hhd", x);
 + 26 //                printf("new value is %c", value);
 + 27 //                fscanf(in, "%hhd", &value);
 + 28 //      }               c = fgetc(in);
 + 29         c = fgetc(in);
 + 30         while(c != EOF)
 + 31         {
 + 32                 if((c >= 65) && (c <= 'Z'))
 + 33                         c = c + keyx;
 + 34                 else if((c >= 'a') && (c <= 'z'))
 + 35                         c = c + keyx;
 + 36
 + 37                 if((c == ('Z' + 1)) || (c == ('z' + 1)))
 + 38                         c = c + keyx;
 + 39
 + 40                 fprintf(out, "%c", c);
 + 41                 fprintf(stdout, "%c", c);
 + 42
 + 43                 c = fgetc(in);
 + 44         }
 + 45         printf("\n");
 + 46         fclose(in);
 + 47         fclose(key);
 + 48         fclose(out);
 + 49         return(0);
 + 50 }
 + 51
 +
 +
 +</code>
 +
 +
 +Decipher code
 +<code>
 +
 +  1 #include<stdio.h>
 +  2 #include<stdlib.h>
 +  3
 +  4 int main()
 +  5 {
 +  6         FILE *in,*out,*key;
 +  7         char c, fname[] = "cipher.txt";
 +  8 //      value = (char*)malloc(sizeof(char) * 250);
 +  9         char keyx =0;
 + 10 //      char x =0;
 + 11         key=fopen("key.txt", "r");
 + 12         in=fopen("cipher.txt", "r");
 + 13         out=fopen("message.txt", "w");
 + 14         if (in == NULL)
 + 15         {
 + 16                 printf("ERROR!\n");
 + 17                 exit(1);
 + 18         }
 + 19 //        fgets(value, 250, in);
 + 20         fscanf(key, "%hhd", &keyx);
 + 21 //        while(*value != EOF)
 + 22 //      {
 + 23 //                printf("original values are %hhd,  ", value);
 + 24 //                x=(value-keyx);
 + 25 //                fprintf(out,"%hhd", x);
 + 26 //                printf("new value is %c", value);
 + 27 //                fscanf(in, "%hhd", &value);
 + 28 //      }               c = fgetc(in);
 + 29         c = fgetc(in);
 + 30         while(c != EOF)
 + 31         {
 + 32                 if((c >= 65) && (c <= 'Z'))
 + 33                         c = c - keyx;
 + 34                 else if((c >= 'a') && (c <= 'z'))
 + 35                         c = c - keyx;
 + 36
 + 37                 if((c == ('Z' + 1)) || (c == ('z' + 1)))
 + 38                         c = c - keyx;
 + 39
 + 40                 fprintf(out, "%c", c);
 + 41                 fprintf(stdout, "%c", c);
 + 42
 + 43                 c = fgetc(in);
 + 44         }
 + 45         printf("\n");
 + 46         fclose(in);
 + 47         fclose(key);
 + 48         fclose(out);
 + 49         return(0);
 + 50 }
 +
 +</code>
 +=====Execution=====
 +
 +<cli>
 +jdavis34@lab46:~/src/cprog$ ./cipher This works finally because matt put up that hint thank you matt
 +please enter a value in which you feel safe:
 +2
 +your secret is safe with me:
 +Vjku yqtmu hkpcnn{ dgecwug ocvv rwv wr vjcv jkpv vjcpm {qw ocvv
 +jdavis34@lab46:~/src/cprog$ ./decipher
 + This works finally because matt put up that hint thank you matt
 +jdavis34@lab46:~/src/cprog$
 +
 +</cli>
 +2nd revision of the more intended main cipher(cipher2)
 +<cli>
 +
 +jdavis34@lab46:~/src/cprog$ echo key.txt > 2
 +jdavis34@lab46:~/src/cprog$ ./cipher2
 +Vjku oguucig ku c vguv vq ugg kh vjku yknn yqtm
 +
 +jdavis34@lab46:~/src/cprog$ ./decipher
 +This message is a test to see if this will work
 +</cli>
 +=====Reflection=====
 +Comments/thoughts generated through performing the project, observations made, analysis rendered, conclusions wrought. What did you learn from doing this project?
 +
 +=====References=====
 +In performing this project, the following resources were referenced:
 +
 +  * Matt?
 +  * Jpettie? the PAlly 
 +  * Hint.c ( help with the fgetc vs using the fscanf or fgets)
 +