XOR encrypt with variable key length
hey all,
I am trying to write a simple string encryptor using XOR based on some code posted here (I think Dave_Sinkula is a member here too): http://www.daniweb.com/code/snippet530.html
here it is, my output from this program is looking like what would be caused by a buffer overrun (a bunch of garbage, note that it is not the XOR'ed string, it is longer than it).
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
unsigned char buf[100];
size_t count, i, j = 0;
count = strlen(argv[1]) + 1;
for(i = 0; i < count; i++) {
buf[i] ^= argv[2][j++];
if(argv[2][j] == '\0')
j = 0;
}
printf("%s", buf);
return 0;
}
argv[1] is obviously the string to encrypt, and argv[2] is the key to encrypt it with. I am trying to do this in a way that the key does not have to be the same length as the string (since thats inconvient when the string is user input).
could anyone please tell me what looks wrong here and what I should do to fix it?
thanks in advance! :D