Thread: Hmm....help me take a look at this: File Encryptor

  1. #1
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36

    Unhappy Hmm....help me take a look at this: File Encryptor

    I am trying to write an simple program to encrypt/decrypt the contents of a text file.

    This is the function that I am using:

    /*
    * Encrypt/Decrypt a file by conducting an XOR operation on the contents
    * of the file and the key provide.
    * Returns 0 if successful and -1 if fail
    *
    * char *filename: The file to be encrypted/decrypted
    * char *key: The key to encrypt/decrypt the file with
    */
    int encrypt (char *filename, char *key)
    {
    FILE *fptR; /* File pointer to read from the file to be encrypted/decrypted */
    FILE *fptW; /* File pointer to write to the file to be encryted/decrypted */
    char reader; /* To store each char read from the file */
    int keysize; /* Size of the key used to encrypt/decrypt */
    int i; /* A counter to iterate the key */
    char out[MAX_STRING]; /* Name of the output file */

    /* Open the file for reading/writing */
    if( ( fptR = fopen(filename, "r+") ) == NULL) {
    printf("ERROR: file reader cannot be open\n");
    return -1;
    }
    if( ( fptW = fopen(filename, "r+") ) == NULL) {
    printf("ERROR: file writer cannot be open\n");
    return -1;
    }

    /* Start Encrypting/decrypting file */
    i = 0;
    keysize = strlen(key);
    reader = fgetc(fptR);
    while(reader != EOF) {
    /* Encrypt with key */
    reader = reader ^ key[i];

    /* Write Encrypted data back to file */
    fputc(reader, fptW);

    /* Update key & file pointers */
    i = (i+1)%keysize;
    reader = (char)fgetc(fptR);
    }

    /* Close files */
    fclose(fptW);
    fclose(fptR);


    return 0;
    }


    It seems to works with a simple "hello world" text file. But when I try to decrypt a C source file, only the first few characters and the last few characters got decypted. The rest inbetween is still rubbish

    Could it be fgetc?

    Thanks in advance
    If only life is as easy as C...

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Haven't tried your file, but here are a few suggestions.

    Perhaps you could open the input file for binary read-only ("rb")and the output file for binary write-only ("wb"), since you don't want to change the input file and want to create a new output file.

    I would treat the bytes as unsigned chars and not as char. So also fgetc's result should be casted to unsigned char. So the bytes of the key, the bytes of the input file and also the bytes of the output file could be declared as unsigned char.

    Further try

    while (!feof (fptR))

    And cast the input for fputc to int.

    fputc ((int) reader, fptW);

  3. #3
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    HEY SHIRO!!!!!!


    ITS WORKING!!

    Thanks alot dude!

    Guess experience really counts here :P

    Once again thank you very much for pointing that out (I spent about 1 hour figuring out whats going on... )

    Anyway, here the file if anyone is interested in it(its really really simple...)
    If only life is as easy as C...

  4. #4
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    Oops, here's the file:
    If only life is as easy as C...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM