#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0

    Program to encrypt a file..


    Here is what I have:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int encryptd(FILE *);
    
    int main(void)
    {
      FILE *fileptr;
      int recode;
    
      char fname[25];
    
      printf("Enter the file name of the file you would like to encrypt:\n");
      scanf("%s", fname);
    
      fileptr = fopen(fname, "rb+");
    
      if(fileptr == NULL)
      {
        printf("Failed to open file.\n");
        return-1;
      }
      else
        fprintf(fileptr,"text\n");
    
      recode=encryptd(fileptr);
    
      fclose(fileptr);
    
      system("pause");
      return 0;
    
    }
    
    int encryptd(FILE *diskfileptr)
    {
      int i;
      unsigned long int file_s;
      int keylen;
      char *filebuff = NULL;
    
      char key[] = "ABCDEF";
      keylen = strlen(key);
    
      fseek(diskfileptr, 0, SEEK_END);
      file_s = ftell(diskfileptr);
    
      rewind(diskfileptr);
    
      filebuff = malloc(file_s);
      if( fread(filebuff, file_s, 1, diskfileptr) != 1)
      {
        printf("Error in reading the file.\n");
        return -1;
      }
    
      for(i=0; i<file_s; i++)
      {
        filebuff[i] = filebuff[i] ^ key[i%keylen];
      }
    
      rewind(diskfileptr);
    
      if( fwrite(filebuff, file_s, 1, diskfileptr) != 1 )
      {
        printf("Error in writing the encrypted data to the file.\n");
        system("pause");
        return -1;
      }
    
      free(filebuff);
      fclose(diskfileptr);
    
      return 0;
    }
    The only problem I seem to be having is that with I run it a second time to decrypt, the first 5 characters in the file are still rubbish. Can someone point me to the error?

    Thanks!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,388
    Rep Power
    1871
    > fprintf(fileptr,"text\n");
    Why do you do this - at all?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    The only thing that really stands out as a potential issue is the uninitialized local variable..

    Code:
    filebuff = malloc(file_s);
    It may contain a lot of garbage prior to being used in your program which may be the source of your problem.

    Try clearing this variable as follows prior to using it...

    Code:
    filebuff = malloc(file_s);
    memset(filebuff, 0, sizeof filebuff);
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0

    Ugh...


    Originally Posted by salem
    > fprintf(fileptr,"text\n");
    Why do you do this - at all?
    It was part of the instructor's example, but after removing it, it works perfect! Thank you!

    Out of curiosity, why did it cause the first 5 characters to be nonsense?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    Last edited by BobS0327; August 26th, 2013 at 10:14 PM.

IMN logo majestic logo threadwatch logo seochat tools logo