I have this code encrypting my file:
static const char* KEY = "Test";
…
FILE* pFile = fopen("skills.dat", "wb+");
char* result = new char[ (TOTAL_SKILLS + 1) * sizeof(int)];
memset(result, 0, (TOTAL_SKILLS + 1) * sizeof(int));
int counter = 0;
while(counter != TOTAL_SKILLS + 1)
{
char szSkillName[40] = "";
strcpy(szSkillName,(buffer + (counter * 0x12C)));
int skillDelay = *(int*)(buffer + (counter * 0x12C) + 228);
*(int*)(result + (counter * 4)) = skillDelay;
counter++;
printf("Skill %s saved.\r\n",szSkillName);
}
CRC4 crc;
crc.Encrypt(result,KEY, (TOTAL_SKILLS + 1) * sizeof(int));
fwrite(result,1, (TOTAL_SKILLS + 1) * sizeof(int),pFile);
fclose(pFile);
the contents of buffer may not be important, the problem is that I can’t decrypt the encrypted data. I am using the same key, obviously, but the decrypted result is not the same as the original input buffer. What might be happening? This sounds weird…
Decryption code:
LPCSTR name = "skills.dat";
int counter = 0;
std::ifstream myFile(name, std::ios::binary | std::ios::out );
if(myFile.fail())
{
myFile.close();
return false;
}
int len = (TOTAL_SKILLS + 1) * sizeof(int);
char* buffer = new char[len];
myFile.read(buffer,len);
myFile.close();
CRC4 crc;
crc.Decrypt(buffer,KEY,len);
I solved my problem by removing the SWAP macro inside the Encrypt function and in the key generation hash.