Problems with Xor encryption
Hey guys, I'm trying to develop a Xor ecnryption function but I'm having some problems...
Here's what I've so far:
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
char string[] = "This is a test!!!";
char key[] = "123";
XorEncrypt (&string, &key);
printf("%s", string);
getch();
return 0;
}
int XorEncrypt (char *string, char *key)
{
int cnt = 0, i, j, r, mod;
int StringLen = strlen(string); // 20
int KeyLen = strlen(key); // 6
r = StringLen / KeyLen;
for (i = 0; i < r; i++)
{
for (j = 0; j < KeyLen; j++)
{
string[i*KeyLen + j] = string[i*KeyLen + j] ^ key[j];
}
}
mod = StringLen % KeyLen;
if ( mod != 0)
{
for (i = 0; i < mod; i++)
{
string[-mod + i] = string[-mod + i] ^ key[i];
}
}
return 0;
}
Output: eZZB↕ZB↕R◄FVBF↕!!
The function is not working tough, if I take that output and pass it again to the function I get this:
a♫test♀!!
I don't know if I made a mistake regarding pointers or if there's something wrong in the function itself... Please help =)