This is one of my first real programs that does anything. I have it pieced together and it is a rough model at the moment. It isn't user friendly yet so bear with me. The file you are encrypting must exist before hand. When I Run Decrypt it Decrypts the first 30 or so characters but then the rest is Jibberish. Please be kind I'm beginning. I also found out having a decrypt function was unnecessary because I can reuse the encrypt.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
void filename_accept(char fileName[]);
void firstpass_encrypt(char string[], char key[],int size);
//void firstpass_decrypt(char key[], char string[], int size);
int position;
int main()
{
FILE *myfile;
FILE *encryptedfile;
char fileName[50];
char userchoice[10];
char key[63]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
char c;
int i = 0;
startagain: ;
filename_accept(fileName);
myfile = fopen(fileName,"r");
fseek(myfile, 0L, SEEK_END);
int size = ftell(myfile);
int linelen = size;
char*string = (char*)malloc(size*sizeof(string));
if (string == NULL)
{
printf("\nMemory Overflow");
exit(-1);
}
/*printf("This is the length %i", size);*/
fseek(myfile, 0L, SEEK_SET);
while((c = fgetc(myfile)) != EOF)
string[i++] = c;
BackToTop: ;
printf("The file we are working with is ");
printf(fileName);
printf("\nAll input is case sensitive until next version!\n");
printf("\nWould you like to Encrypt or Decrypt? \nPlease type one of the following: \n 1 Encrypt for Encrypt \n 2 Decrypt for Decrypt\n 3 Quit to terminate program ");
printf("\n 4 Type again to open a different file.\n");
fflush(stdin);
if (fgets(userchoice, sizeof userchoice, stdin) != NULL);
{
char *newline = strchr(userchoice, '\n'); //search for newline
if (newline != NULL)
*newline = '\0'; //overwrite newline
}
int strcheck1 = strcmp(userchoice, "Encrypt");
int strcheck2 = strcmp(userchoice, "Decrypt");
int strcheck3 = strcmp(userchoice, "quit");
int strcheck4 = strcmp(userchoice, "Again");
if(strcheck1 == 0)
{
firstpass_encrypt(string, key, size);
/*printf("%c", string[position]);*/
strcat(userchoice, ".txt"); //adds .txt format to it
encryptedfile = fopen(userchoice, "w");
for(int x=0; x <= size; x++)
fprintf(encryptedfile, "%c", string[x]);
printf("\nYour file has been encrypted.");
fclose(encryptedfile);
}
else if(strcheck2 == 0)
{
firstpass_encrypt(key, string, size);
strcat(userchoice, ".txt");
encryptedfile = fopen(userchoice, "w");
for (int x=0; x <= size; x++)
fprintf(encryptedfile, "%c", string[x]);
printf("\nYour file has been decrypted");
fclose(encryptedfile);
}
else if(strcheck3 == 0)
return 0;
else if(strcheck4 == 0)
goto startagain;
else
{
printf("%i", strcheck1);
printf("%i", strcheck2);
printf("\nInvalid Input. Please follow the specified format");
goto BackToTop;
}
//printf("\n\n%i", position);
free(string);
fclose(myfile);
fclose(encryptedfile);
system("pause");
return 0;
}
void filename_accept(char fileName[]) /*Reads in file name*/
{
printf("\nEnter file you would like to open:\n");
gets(fileName);
}
void firstpass_encrypt(char string[], char key[],int size)
{
for(position=0; position < size; position++)
{
if(string[position] == '\0')
break;
else
string[position]=string[position]^key[position%64];
}
}
/*void firstpass_decrypt(char key[], char string[], int size)
{
for(position = 0; position < size;position++)
{
if (string[position] == '\0')
break;
else
string[position]=key[position%64]^string[position];
}
}*/