Hello everyone.
I made this "text file de/encryption" program as my first big C project.
It's working pretty decent, and takes a lot of special characters and tabs without any problems.
I have tested the code by encrypting and then decrypting some source code, and compared the output with the original file, using this tool: http://www.textdiff.com and the result where total identical.
Still have some bugs, and things to come (details in source).
As of now I can only give "one" option like this: "./crypter -e file.txt" or like this "./crypter -er file.txt" (-r would be the replace function, not jet implemented). Because the code relies on argv[2] being the input file, so I can't give options like this (./crypter -e -r file.txt).
So I'm looking for some kind of options structure that would be more efficient, and at the same time fix the last bug, where the input file is checked and opened and output file is opened before the actual options are verified.
I know from BASH that option menu’s can be done very sophisticated and clean. I doubt that there are implementations that will make C this clean and simple, but hoping for something better than my solution.
I realize that I may need to restructure the entire code, because as of now, the encrypt and decrypt function is almost identical, and the main function is really messy.
#include <stdio.h>
#include <stdlib.h>
/* ENCRYPTING FILES
15.Dec.2012
By Morten Lund
Thing to add:
# Progress bar.
# Create options menu. (Rethink the hole "options given" concept.
# Option to delete and replace source file. (-r)
Bugs:
# FIXED 12.Jan.2013 - Not posible to en/decrypt numbers.
# FIXED 15.Jan.2013 - Not posible to en/decrypt "new line" & "tab".
# FIXED 15.Jan.2013 - Not posible to en/decrypt "special chars".
#
# If valid "input file" is given but nonvalid option, output file will still be opened.
*/
#define ALPHA "\\~!?=#)%(&[{]}|@,.;:-_*^+<>1234567890abcdefghijklmnopqrstuvwxyzABCDEFG HIJKLMNOPQRSTUVWXYZ/"
#define CRYPT_ALPHA "/#UwVM{>12E+j,4amN&stin]Ao_6}Q|7q[!f(FJKcLBz^)XYdhZkl9e?Wv@u;35yHrg< P*pSGTx=Ib~%R0O.8:-DC\\"
#define ALPHA_SIZE sizeof(ALPHA)
void encrypt(FILE *fpIN, FILE *fpOUT); // decrypting file
void decrypt(FILE *fpIN, FILE *fpOUT); // encrypting file
void wrongUse(); // Error msg
int main(int argc, char *argv[]) {
if (sizeof(ALPHA) != sizeof(CRYPT_ALPHA)) {
printf("Normal and cryptic alphabet is not the same size!!");
return 1;
}
FILE *fpIN; // Filepointer to input file
FILE *fpOUT; // Filepointer to output file
if (argv[1] != NULL && argv[2] != NULL) { // If argument 1 and 2 is given
if ((fpIN = fopen(argv[2], "r")) != NULL) { // If argument 2 is a readable file & open it
// If no "output file" is given, it's set to default: out.txt
if (argv[3] == NULL) {
if (strcmp(argv[2], "out.txt") == 0)
fpOUT = fopen("out2.txt", "w");
else
fpOUT = fopen("out.txt", "w");
}
else
fpOUT = fopen(argv[3], "w");
// Running encrypt or decrypt function, depending on what argument 1 (option)
if (strcmp(argv[1], "-e") == 0)
encrypt(fpIN, fpOUT);
else if (strcmp(argv[1], "-d") == 0)
decrypt(fpIN, fpOUT);
else
wrongUse();
// Closing input and output pointer after en/decrypt functions
fclose(fpIN);
fclose(fpOUT);
} else
wrongUse();
} else
wrongUse();
return EXIT_SUCCESS;
}
void encrypt(FILE *fpIN, FILE *fpOUT) {
char curChar; int i, encryptFound;
while ((curChar = fgetc(fpIN)) != EOF) {
for (i = 0; i < ALPHA_SIZE; i++) {
if (curChar == ALPHA[i])
fputc(CRYPT_ALPHA[i], fpOUT);
}
if (curChar == '\n')
fputc('\n', fpOUT);
if (curChar == '\t')
fputc('\t', fpOUT);
}
}
void decrypt(FILE *fpIN, FILE *fpOUT) {
char curChar; int i;
while ((curChar = fgetc(fpIN)) != EOF) {
for (i = 0; i < ALPHA_SIZE; i++) {
if (curChar == CRYPT_ALPHA[i])
fputc(ALPHA[i], fpOUT);
}
if (curChar == '\n')
fputc('\n', fpOUT);
if (curChar == '\t')
fputc('\t', fpOUT);
}
}
void wrongUse() {
printf("\nUse: option fileIN fileOUT\n\nfileOUT is optional\nif no options is given fileOUT will be named out.txt\nif fileIN is named out.txt, fileOUT will be named out2.txt\n\nOptions:\n-e = encrypt\n-d = decrypt\n");
}