I was learning about cryptography using c++, so i get started with very simple XOR, i want to open file 0x1.txt and encrypt it using XOR with 3 keys, and create a new file named 0x2.txt and put the encrypted data into it , and decrypt it and put its content in 0x3.txt:
encrpyt -> 0x1.txt -->put encrypted data in 0x2.txt ; decrypt 0x2.txt --> put decryped data in 0x3.txt
and here is my code :
encrypt code :
LPVOID Crypt(HANDLE hFile, DWORD dwFileSize) {
// allocate buffer for file contents
LPVOID lpFileBytes = malloc(dwFileSize);
// read the file into the buffer
ReadFile(hFile, lpFileBytes, dwFileSize, NULL, NULL);
// apply XOR encryption
int i;
char key[3] = {'*', '~', '#'};
for (i = 0; i < dwFileSize; i++) {
*((LPBYTE)lpFileBytes + i) ^= key[i % sizeof(key)];
}
return lpFileBytes;
}
calling the function to encrypt the file:
HANDLE hFile = CreateFile("0x1.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// get file size
DWORD dwFileSize = GetFileSize(hFile, NULL);
CloseHandle(hFile);
// crypt and get crypted bytes
LPVOID lpFileBytes = Crypt(hFile, dwFileSize);
then put the encrpyed data in 0x2.txt :
HANDLE hCryptedFile = CreateFile("0x2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// write to crypted file
WriteFile(hCryptedFile, lpFileBytes, dwFileSize, NULL, NULL);
Now i want to decrypt the content of 0x2.txt file i made this :
HANDLE hFile = CreateFile("0x2.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// get file size
DWORD dwFileSize = GetFileSize(hFile, NULL);
// decrypt and obtain decrypted bytes
LPVOID lpFileBytes = Crypt(hFile, dwFileSize);
CloseHandle(hFile);
create file 0x3.txt:
HANDLE hTempFile = CreateFile("0x3.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// write to temporary file
WriteFile(hTempFile, lpFileBytes, dwFileSize, NULL, NULL);
// clean up
CloseHandle(hTempFile);
free(lpFileBytes);
but the file, it encrypt more , not decrypt !. so what its the problem ? here is my full code:
https://pastebin.com/6WZX5J1K