A project for C++ by Michael during the Spring 2012.
This project was begun on feb 14 and is anticipated to take 2 weeks to complete. Project was completed on Feb 28th, 2012
We are making an encoder and decoder in C.
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
Writting a program that will encode a text file by chagning the characters.
A different file will decode a text file by reversing the change in characters.
It asks for a cypher key, and you will need to know that key when you decode it.
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:
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:
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).
The encipher code:
/*encodes*/ #include<stdio.h> #include<stdlib.h> int main() { FILE *original, *encoded; int key = 0; //number of asci spaces moved int holder = 0; //used as a temp space holder char letter; original = fopen("file2.txt","r"); encoded = fopen("encoded.txt","w"); printf("Please pick your encyphering number: "); scanf("%d",&key); letter = fgetc(original); while(letter != EOF) { if ((letter >='A') && (letter <= 'Z')) { holder = letter - 65; holder = holder + key; holder = holder % 26; letter = holder + 65; } if ((letter >= 'a') && (letter <= 'z')) { holder = letter - 97; holder = holder + key; holder = holder % 26; letter = holder + 97; } fprintf(encoded, "%c", letter); letter = fgetc(original); } printf("Encoding complete\n"); fclose(original); fclose(encoded); return(0); }
Deciphering code.
//decodes #include<stdio.h> #include<stdlib.h> int main() { FILE *encoded, *decoded; char typed; int key = 0; char letter; unsigned char holder = 0; encoded = fopen("encoded.txt", "r"); decoded = fopen("decoded.txt", "w"); printf("Please enter the original key number: "); scanf("%d", &key); letter = fgetc(encoded); while(letter != EOF) { if ((letter >= 'A') && (letter <= 'Z')) { holder = letter - 65; holder = holder - key - 230; holder = holder % 26; letter = holder + 65; } if ((letter >= 'a') && (letter <= 'z')) { holder = letter - 97; holder = holder - key - 230; holder = holder % 26; letter = holder + 97; } fprintf(decoded, "%c", letter); letter = fgetc(encoded); } printf("Decoding complete, check decoded.txt \n"); fclose(encoded); fclose(decoded); return(0); }
An example run of the enciphering process:
Please pick your encyphering number: 4 Encoding complete
Example run of the decoding process:
Please enter the original key number: 4 Decoding complete, check decoded.txt
I learned that this project was a pain in the butt. I also know, I don't want to mess with stuff like this again. It gave me a headache trying to figure it out, and I don't think I could easly rewrite this program.
In this project, I had a lot of help from Matt, our teacher. I had so many issues that I couldn't figure out why they were happening.