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

Try reading and writing in binary mode

I know from experience, that encryption programs have problems if you're using just .txt files.

Reading and writing in binary mode with a .dat file worked for me.

Since only you know how the encryption works, we can only guess. And my guess is:
If your encryption changes a valid character to 0x1A, you get EOF in your file. To solve this, change your encrypted file to a binary file instead of text and that should solve your problem.

Hiya,

Thanks for your replies. I can say that i did try changing the extension to '.dat' however, I didn't realize that you could change the way the file is written. How do I change to writing in binary mode?

Regards

Craig

First of all you open the file in binary mode:

input = fopen(argv[3], "rb");
output = fopen(argv[4], "wb");

and then using fwrite function.

Thanks for all the help...

I'm new to the fread and fwrite functions however, i have managed to get most of it working to try and keep it simple for testing i have made this algorithm.

while(fread(file,128,1, input))
                 {
                      
                       encrypt(key,file,cipher); //function that encrypts 128 bits at a time
                       
                       decrypt(key,plain,cipher);//function that decrypts128 bits at a time

                      fwrite(plain, 128, 1,output);

                }

this works however,cuts about 100 bits of the end. I thought it was because the while loop exits before the last bytes have been written so i added a fwrite outside of the loop but it writes the full 128 bites that were in the array. Is there a way of finding out how many bytes there are left before it reached the EOF marker so that i can set a variable? Or have a gone completely in the wrong direction and there is a super easy way to make this work?

thanks again.

Try something like this after the loop:

printf("current position = %d\n", curr = ftell(file));
fseek(file, SEEK_END, 1); /* go to end of file */
printf("last position = %d\n", last = ftell(file));
printf("left = %d\n", last - curr);
This article has been dead for over six months. Start a new discussion instead.