Okay, I've been following a thread on here where an AO user explained XOR to me (I forget who, sorry). But anyways, I wrote a simple C++ app that takes an input file, and XOR's it against the corresponding bytes of another file, and outputs it to an encrypted file...but my problem is, why doesn't it decrypt correctly...when I encrypted a TXT file with "hello", i got some weird ASCII characters...expected. But when I decrypted it, I got "helloou". I used the key "this is garbage.".
Zeroth remark: It was [SirDice/]Tim_axe...
First remark: Assign the main-function with a return type.
Second remark: Avoid any call to [f]eof. I always have trouble with it.
Here in this case, it spoils the length of the input by 1. Hence, your
decrypted file has a length +2 in the end.
Third remark: Avoid to read in byte by byte. The performance is bad.
Read the whole thing in in one operation. Here is a working code:
I notice that the buffer sizes are only 30...without using eof(), how would I read a file larger than 30 bytes without changing the buffer's size.
Also, how can I dynamically allocate memory to be the size of a particular file? I've messed with malloc(), but I'm not fully aware of how to use it correctly.
A_T
PS - I have not attempted to compile this code yet....I'm on a fresh Windows install on another PC.
February 23rd, 2005, 06:46 PM
sec_ware
Hi A_T
You really should do some reading. Solving a little problem by yourself
helps you to keep that stuff in mind - hence, you will make progress.
Copy and pasting prepared code, even when you "look" at it, does not
help to improve your skills. Be independent!
Anyway, two ways to get the filesize, after that a little malloc
malloc_buf = (char *)malloc( _filelength(fh) ); // convert the type of memory to char* rather than void*
printf("Bytes _read: %ld\n", _read( fh, malloc_buf, _filelength(fh) ) );
free(malloc_buf);
_close( fh );
Note, that the number of bytes read can be smaller than the filelength.
This is due to a reinterpretation of 0x10h and 0x13h. You can use the
filelength, determined by the low-level io routines, as an input of the
istream of course.
Cheers.
February 24th, 2005, 01:42 AM
AxessTerminated
I prefer not to read from a book when I can get more competent questions here, ones that I can inquire more if I need to.
But thanks for the answer.
A_T