![]() |
| | #1 |
| Registered User Join Date: Mar 2002
Posts: 36
| 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... |
| heljy is offline | |
| | #2 |
| .... Join Date: Aug 2001 Location: Groningen (NL)
Posts: 2,386
| 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); |
| Shiro is offline | |
| | #3 |
| Registered User 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... |
| heljy is offline | |
| | #4 |
| Registered User Join Date: Mar 2002
Posts: 36
| Oops, here's the file:
__________________ If only life is as easy as C... |
| heljy is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problems passing a file pointer to functions | smitchell | C Programming | 4 | 09-30-2008 02:29 PM |
| sequential file program | needhelpbad | C Programming | 80 | 06-08-2008 01:04 PM |
| Formatting the contents of a text file | dagorsul | C++ Programming | 2 | 04-29-2008 12:36 PM |
| archive format | Nor | A Brief History of Cprogramming.com | 0 | 08-05-2003 07:01 PM |
| Making a LIB file from a DEF file for a DLL | JMPACS | C++ Programming | 0 | 08-02-2003 08:19 PM |