Hi there,
I'm trying to write a simple encryption program that reads normal text from a txt file - encrypts it using a Vigenère Table - then outputs the results to another file. This process should obviously be reversible as well.
I have written this for the encryption part of the program:
input = fopen(argv[3], "r");
output = fopen(argv[4], "w");
flag = 0;
while(flag==0)
{
for(i=0;i<MAX;i++)
{
if((c = getc(input)) != EOF)
{
file[i] = c;
}
else
{
file[i] = -1;
break;
}
}
encrypt(key,file,cipher);
for(i=0;i<MAX;i++)
{
if(cipher[i]!=-1)
{
fprintf(output,"%c",cipher[i]);
}
else
{
fprintf(output,"%c",cipher[i]);
i = MAX;
flag = 1; //this a flag that exits a while loop not included in this code.
}
}
}
fclose(input);
fclose(output);
The 'encrypt' function looks like this:
void encrypt(char *key,char *plainTxt,char *cipherTxt)
{
int i,vig[MAX][MAX];
char a,b;
createVig(vig); // Creates a standard Vig 128 x 128 table
for(i=0;i<MAX;i++) // Loops for 128 characters
{
a = key[i]; // Assigns the value of key[i] to a
b = plainTxt[i];
if(b!=-1)
{
cipherTxt[i] = vig[a][b]; // Finds the new cipherTxt character and assigns it to cipherTxt[i]
}
else
{
cipherTxt[i]=-1;
i = MAX;
}
}
}
I decrypt in almost the same way with a few differences. The problem im having is when I decrypt from the cipher text file it only gets about 200 characters in and then finds an EOF (-1) in the text file although there isnt one there.
I tested my algorithms by encrypting and decrypting without writing them to a file and they worked fine. I believe this has narrowed it down to how it is written to the file however, I don't know of any other ways in which to do it.
I hope that made sense to some one out there. Any help would be greatly appreciated.
Many Thanks
Craig