So I gather you're experimenting with XOR encryption? (Sometimes it's easier -- and better -- to state the problem, and/or the "big picture", rather than perceived issues with assumed solutions.) Anyways, a number of your latest posts seems to point me toward that.
And it's been kinda slow, and I've never written one that I can remember, so I decided to play along at home. Here is something to compare with after you've finished or as you continue developing your code. I'm sure others will be along soon to nitpick it as well.
Code:
#include <stdio.h>
/*
* argv[1] - name of input file to encrypt/decrypt
* argv[2] - name of output file
* argv[3] - crypt key
*/
int main(int argc, char *const *argv)
{
if ( argc == 4 )
{
FILE *input = fopen(argv[1], "rb");
FILE *output = fopen(argv[2], "wb");
if ( input != NULL && output != NULL )
{
unsigned char buffer[BUFSIZ];
size_t count, i, j = 0;
do {
count = fread(buffer, sizeof *buffer, sizeof buffer, input);
for ( i = 0; i < count; ++i )
{
buffer[i] ^= argv[3][j++];
if ( argv[3][j] == '\0' )
{
j = 0; /* restart at the beginning of the key */
}
}
fwrite(buffer, sizeof *buffer, count, output);
} while ( count == sizeof buffer );
fclose(input);
fclose(output);
}
}
return 0;
}
Doing something like this...
H:\Forums>xorcrypt main.c file.bin key
H:\Forums>xorcrypt file.bin file.txt key
You should find that main.c matches file.txt.