Thank you for your quick response buck. I tried doing as you said, but for some reason the text at the file remains unchanged...
Here's what I'm using:
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
long fSize;
size_t result;
char key[] = "123", *buffer;
fp = fopen ("xor.txt", "rb+");
if (fp == NULL)
{fputs("File error",stderr); exit(1);}
fseek (fp, 0, SEEK_END);
fSize = ftell (fp);
rewind (fp);
buffer = (char*) malloc (sizeof(char)*fSize);
if (buffer == NULL)
{fputs ("Memory error",stderr); exit (2);}
result = fread (buffer, 1, fSize, fp);
if (result != fSize)
{fputs ("Reading error",stderr); exit (3);}
XorEncrypt(buffer, &key);
fwrite (buffer, 1, fSize, fp);
fclose(fp);
free(buffer);
// getch();
return 0;
}
int XorEncrypt (char *string, char *key)
{
int i, j, mod;
int StringLen = strlen(string); // 20
int KeyLen = strlen(key); // 6
for (i = 0; i < (StringLen / KeyLen); i++)
{
for (j = 0; j < KeyLen; j++)
{
string[i*KeyLen + j] ^= key[j];
}
}
mod = StringLen % KeyLen;
if (!mod)
{
for (i = 0; i < mod; i++)
{
string[-mod + i] ^= key[i];
}
}
return 0;
}