Re: Encrypt/Decrypt Files



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

.



Relevant Pages

  • Re: Encrypt/Decrypt Files
    ... I'm having problem with my simple encrypt/decrypt program. ... What is 'sTmpString' pointing to? ... UCHAR* Decrypt ... BOOL EncryptFile(char* sSrcFilename, char* sDestFilename, UINT key) ...
    (comp.os.msdos.programmer)
  • Re: Decrypt problem using Rijndael
    ... you cannot use Unicode to convert between bytes and strings - replace all the conversions with Convert.From/To Base64String ... I'm using an algo to encrypt/decrypt an xml file using Rijndael. ... of the data to decrypt is invalid" ...
    (microsoft.public.dotnet.security)
  • Decrypt problem using Rijndael
    ... I'm using an algo to encrypt/decrypt an xml file using Rijndael. ... However, SOMETIMES the decrypt algo fails when I try to read the data in cryptostream: ...
    (microsoft.public.dotnet.security)
  • RE: Decryption using private key from cert store failing with err
    ... The reason we need to do a decrypt/encryptis that that is ... It decrypts with the private key and then the ... The other libraries encrypt/decrypt calls do not ... > invalid cryptograph to decrypt, otherwise we will get invalid data ...
    (microsoft.public.platformsdk.security)
  • Re: Failed to decrypt
    ... If you are using the new encrypt/decrypt methods of the IO.File be aware that only the user that encrypts can decrypt. ... "Failed to decrypt using provider 'RsaProtectedConfigurationProvider'. ... Error message from the provider. ...
    (microsoft.public.dotnet.languages.vb)