C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-23-2002, 05:10 AM   #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...
heljy is offline   Reply With Quote
Old 03-23-2002, 06:31 AM   #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   Reply With Quote
Old 03-23-2002, 10:53 AM   #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...
heljy is offline   Reply With Quote
Old 03-23-2002, 10:57 AM   #4
Registered User
 
heljy's Avatar
 
Join Date: Mar 2002
Posts: 36
Oops, here's the file:
Attached Files
File Type: c encryptor.c (5.8 KB, 44 views)
__________________
If only life is as easy as C...
heljy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:19 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22