Re: Encrypt/Decrypt Files
- From: Sonny <smaniaol@xxxxxxxxx>
- Date: Thu, 05 Jul 2007 06:22:51 -0000
On Jul 5, 1:15 pm, p...@xxxxxxxxxxxxxxxxxx wrote:
In article <1183597415.205148.12...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
smani...@xxxxxxxxx " Sonny" writes:
Hello
I'm having problem with my simple encrypt/decrypt program. The purpose
of my program is to encrypt a text file (test.txt) into a binary file
(result.bin), then decrypt that binary file back to a text file
(res.txt) again. I think I'm having problems with the EOF flag. I
always get extra characters at the end of the file in the decrypted
file
There are several things in your code that you should look at,
but the "EOF flag" (whatever that might be) is not one of them.
I'm surprised that you're getting any result at all without the
program crashing... I would guess that the reason for your extra
characters after decryption is being caused by using standard
C string functions for manipulating binary data (which will not
be zero-termainated and will likely have embedded '\0' bytes).
Some more comments below.
Here is my code:
[snip]
UCHAR* Encrypt(UCHAR* sTextString, UINT key)
{
UCHAR* sTmpString;
What is 'sTmpString' pointing to? You should malloc() a buffer
for it (and have caller free() it) or, since the output is the
same size as the input you could even overwrite the input.
for (register int idx=0; idx < strlen(sTextString); idx++)
You are computing strlen() for every iteration, which will cost
you in performance; better to calculate it once at the start and
use that in your loop.
sTmpString[idx] = sTextString[idx] + key;
//sTmpString[idx] = sTextString[idx];
return sTmpString;
}
UCHAR* Decrypt(UCHAR* sTextString, UINT key)
{
UCHAR* sTmpString;
Same comments as above.
for (register int idx=0; idx < strlen(sTextString); idx++)
sTmpString[idx] = sTextString[idx] - key;
//sTmpString[idx] = sTextString[idx];
return sTmpString;
}
BOOL EncryptFile(char* sSrcFilename, char* sDestFilename, UINT key)
{
UCHAR* sTmpString;
int length;
Shouldn't this be a 'long'? (It would be in C, not sure of C++)
ifstream srcFile(sSrcFilename, ios::in);
ofstream destFile(sDestFilename, ios::out| ios::binary);
if ( (!srcFile) || (!destFile) ) return FALSE;
// get length of file
srcFile.seekg (0, ios::end);
length = srcFile.tellg();
srcFile.seekg (0);
sTmpString = (UCHAR *) calloc(length+1, sizeof(UCHAR));
Good!
memset(sTmpString,'\0',length+1);
But this is unnecessary with calloc()...
srcFile.read(sTmpString,length);
destFile.write(Encrypt(sTmpString,key),length);
srcFile.close();
destFile.close();
free(sTmpString);
return TRUE;
}
BOOL DecryptFile(char* sSrcFilename, char* sDestFilename, UINT key)
{
[snip rest]
Same comments as 'EncryptFile()'.
Pls take note of the functions Encrypt and Decrypt. I have commented a
line there which just copies the string without applying the key. If i
use this instead of the previous line, it will encrypt and decrypt
without errors (extra characters). What should I do? Thanks in
advance!
Don't rely on strlen/strcpy etc. for binary data. I'd suggest
you pass a third size param to your Encrypt/Decrypt functions and
use that for malloc() and loop control. No doubt others will
point out the bits I missed in this quick scan, but you should
have a few things to try.
Pete
--
"We have not inherited the earth from our ancestors,
we have borrowed it from our descendants."
thanks guys, i'll clean up my codes. fyi i'm using turbo c++ 3.0
.
- Follow-Ups:
- Re: Encrypt/Decrypt Files
- From: pete
- Re: Encrypt/Decrypt Files
- References:
- Encrypt/Decrypt Files
- From: Sonny
- Re: Encrypt/Decrypt Files
- From: pete
- Encrypt/Decrypt Files
- Prev by Date: Re: Encrypt/Decrypt Files
- Next by Date: Re: Encrypt/Decrypt Files
- Previous by thread: Re: Encrypt/Decrypt Files
- Next by thread: Re: Encrypt/Decrypt Files
- Index(es):
Relevant Pages
|