Thread: Simple File encryption

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Simple File encryption

    So when I use fgets in this code. the output seems to have an extra character. I"m not very experienced in using fgest so I was wondering if maybe someone would enlighten me as to why i get the extra characters. The are ascii value 11 and 12 one on encryption and the other on decryption respectively. Thanks.
    Code:
    /*simple encryption and decryption*/
    #include <stdio.h>
    void Encrypt(FILE *);
    void Decrypt (FILE *);
    void Show (FILE *);
    
    int main(int argc, char *argv[]){
        
        /*open the name of the file to play with */
        FILE *fp;
        int i;
        char buffer[BUFSIZ];
        char *find;
        
        
        
        
        if ( (fp = fopen(argv[1], "w+") ) == NULL){
            perror("Error");
            getchar();
            return 1;
        }
        
        
        fputs("Enter some text to add to the file",stdout);
        fputs(" Press [ENTER] twice when done",stdout);
        putchar('\n');
       
        
       /*using gets
        while ( (gets(buffer) )!=NULL && buffer[0] !='\0')
           fputs(buffer,fp);
       */
        
        
        
      while ( ( fgets(buffer, BUFSIZ, stdin) ) != NULL && buffer[0] !='\n'){
            fputs(buffer,fp);
        } 
        
        //take out the '\n' in buffer    
         if( ( find = strchr(buffer,'\n') ) !=NULL)
            *find = '\0'; 
       
         /* alternative to taking out the '\n' 
         
           int j;
           j = strcspn(buffer, "\n");
           buffer[j] = '\0'; 
         
         */
           
        puts("Press enter to Encrypt the file");
        getchar();
        Encrypt(fp);
        puts("This is the file encrypted:");
        Show(fp);
      
        puts("press enter to decrypt the file");
        getchar();
        Decrypt(fp);
        puts("This is the original contents of the file:");
        Show(fp);
        
        puts("Press enter to close the file");
        getchar();
        if ( ( fclose(fp) ) != 0)
           perror("Error");
         
        puts("File closed successfully!");
        
        
        getchar();
        return 0;
    }  
    
    /*Encrypt the file simply */
    void Encrypt(FILE *file){
        rewind(file);
        /*go through the file and change all characters to 1 charater after */
        int i;
        while ( ( i = fgetc(file) )!=EOF ){
            i = i + 1;
            fseek(file, -1, SEEK_CUR);
            fputc(i,file);
            //fflush(file);
            //fflush not needed if going back to original position in the file
            fseek(file, 0, SEEK_CUR);
        }
    }
    
    void Decrypt(FILE *file){
        rewind(file);
        /*go through the file and change all characters back from 1 */
        rewind(file);
        int i;
        while ( ( i = fgetc(file) )!=EOF ){
            i = i - 1;
            fseek(file, -1, SEEK_CUR);
            fputc(i,file);
            //fflush(file);
            /* fflush not needed if going back to the original postion in the file
             use one or the other */
            fseek(file, 0, SEEK_CUR);
        }
    }     
    
    
    void Show (FILE *file){
        rewind(file);
        fprintf(stdout,"Here is the contents of your file:\n");
        /*stupid! print out the name of the file in question being manipulated */
        
        int i;
        while ( ( i = fgetc(file) )!=EOF )
            fputc(i,stdout);
            
        putchar('\n');
        
    }
    This is output on a trial run:

    Enter some text to add to the file Press [ENTER] twice when done
    abcdef1234

    Press enter to Encrypt the file

    This is the file encrypted:
    Here is the contents of your file:
    ?cdefg2345
    press enter to decrypt the file

    This is the original contents of the file:
    Here is the contents of your file:
    abcdef1234?

    Press enter to close the file
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    im thinking, your extra character comes from the fact
    that your '\n' is still getting printed to the file.

    id suggest, removing the putchar('\n') statement
    or whatever it was and try the program again,
    i could be wrong i just glanced at it, but if tried to
    make a program like this before, and had similar
    problems.

    here is one of my very first projects, that i havent
    worked on in a while, but the source is included
    so you can check it out and see if it helps you any :

    http://jarjar.endofinternet.org/encryption.rar

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Never mind My brain was acting up. Simple logic problem. In previous code The call to strchr was ineffective due to its placement in the code. As a result the '\n' from each call to fgets was stored in the buffer.
    For history's sake here is the refactored code. One can always use fwrite and fread to faciliate I/O as well.
    PHP Code:
    /*simple encryption and decryption*/
    #include <stdio.h>
    void Encrypt(FILE *);
    void Decrypt (FILE *);
    void Show (FILE *);

    int main(int argcchar *argv[]){
        
        
    /*open the name of the file to play with */
        
    FILE *fp;
        
    int i;
        
    char buffer[BUFSIZ];
        
    char *find;
        
        if ( (
    fp fopen(argv[1], "w+") ) == NULL){
            
    perror("Error");
            
    getchar();
            return 
    1;
        }
        
        
        
    fputs("Enter some text to add to the file",stdout);
        
    fputs(" Press [ENTER] twice when done",stdout);
        
    putchar('\n');
       
        
       
    /*using gets
        while ( (gets(buffer) )!=NULL && buffer[0] !='\0')
           fputs(buffer,fp);
       */
        
        
    while ( ( fgets(bufferBUFSIZstdin) ) != NULL && buffer[0] !='\n'){
          if( ( 
    find strchr(buffer,'\n') ) !=NULL)
            *
    find '\0'
            
    fputs(buffer,fp);
        }   
    /* alternative to taking out the '\n' 
            int j;
           j = strcspn(buffer, "\n");
           buffer[j] = '\0'; 
         
    */     
        
    puts("Press enter to Encrypt the file");
        
    getchar();
        
    Encrypt(fp);
        
    puts("This is the file encrypted:");
        
    Show(fp);
      
        
    puts("press enter to decrypt the file");
        
    getchar();
        
    Decrypt(fp);
        
    puts("This is the original contents of the file:");
        
    Show(fp);
        
        
    puts("Press enter to close the file");
        
    getchar();
        if ( ( 
    fclose(fp) ) != 0)
           
    perror("Error");
         
        
    puts("File closed successfully!");
        
        
        
    getchar();
        return 
    0;
    }  

    /*Encrypt the file simply */
    void Encrypt(FILE *file){
        
    rewind(file);
        
    /*go through the file and change all characters to 1 charater after */
        
    int i;
        while ( ( 
    fgetc(file) )!=EOF ){
            
    1;
            
    fseek(file, -1SEEK_CUR);
            
    fputc(i,file);
            
    //fflush(file);
            /* fflush not needed if making a call to fseek. Either one is needed for 
            manipulating a stream with i/o and vice versa */
            
    fseek(file0SEEK_CUR);
        }
    }

    void Decrypt(FILE *file){
        
    rewind(file);
        
    /*go through the file and change all characters back from 1 */
        
    rewind(file);
        
    int i;
        while ( ( 
    fgetc(file) )!=EOF ){
            
    1;
            
    fseek(file, -1SEEK_CUR);
            
    fputc(i,file);
            
    //fflush(file);
           /* fflush not needed if making a call to fseek. Either one is needed for 
            manipulating a stream with i/o and vice versa */
            
    fseek(file0SEEK_CUR);
        }
    }     


    void Show (FILE *file){
        
    rewind(file);
        
    fprintf(stdout,"Here is the contents of your file:\n");
        
        
    int i;
        while ( ( 
    fgetc(file) )!=EOF )
            
    fputc(i,stdout);
            
        
    putchar('\n');
        

    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM