yeah I realized that.
And why are you using loop instead of simple linear modification of the rand() value to bring it into desired interval?
what do you mean? the way I'm doing it seems the most simple...
it works now, however someway, somehow, my encryption/decryption is getting screwed up.
here is the code I am using to encrypt the data:
Code:
while(ReadFile(hFile, buf, BUF_SIZE, &dwBytesRead, NULL) && dwBytesRead > 0) {
EncryptRecord(buf, dwBytesRead, szKey);
WriteFile(hStub, buf, dwBytesRead, &dwBytesWritten, NULL);
if(dwBytesWritten != dwBytesRead)
return 0;
}
it is in a loop, but note that there is not a new random key generated for each iterate, this random key is generated before this loop is encountered and will remain the same for each iterate.
and here is the code I am using to decrypt it:
Code:
ReadFile(hStub, buf, pfile_data->size, &dwBytesRead, NULL);
wsprintf(tstbuf, "read %d bytes", dwBytesRead);
MessageBox(NULL, tstbuf, NULL, MB_OK);
DecryptRecord(buf, dwBytesRead, szKey);
WriteFile(hFile, buf, dwBytesRead, &dwBytesWritten, NULL);
if(dwBytesWritten != dwBytesRead) {
wsprintf(tstbuf, "%x", GetLastError());
MessageBox(NULL, tstbuf, NULL, MB_OK);
return 2;
}
I try this with simple text files and it only decrypts the first 4 bytes . I see absolutely no reason why this shouldn't work, I have already posted the EncryptRecord and DecryptRecord functions here. I really can't see how these functions could encounter different bytes. both functions work the same way, they should both be encrypting/decrypting the same 4 bytes of data with the same 4 bytes of the key.
szKey in both the encryptor and decryptor is declared as:
and gets null terminated before the key even gets added into it:
the for loop in both EncryptRecord and DecryptRecord should be encountering the same bytes at the same location, as is happening with the first 4 bytes, but I literately can't see how the next 4 bytes could be any different here .
EDIT: also note that the messagebox statements I inserted everywhere print the same keys for the files in both the encrypter and the decrypter. not the same key for every file (each file has its own random key), but the correct keys for their corresponding files. for example:
key for file1 in encrypter prints 7683
key for file1 in decrypter prints 7683
key for file2 in encrypter prints 8721
key for file2 in decrypter prints 8721
I wouldn't be getting the first 4 bytes of the file decrypted if they were different.