KyleBanks/XOREncryption

there is a bug with the c version

Opened this issue · 0 comments

I had encountered a bug with the c version code when I had tried to decrypt the encrypted string.

void encryptDecrypt(char *input, char *output) {
	char key[] = {'K', 'C', 'Q'}; //Can be any chars, and any size array
	
	int i;
	for(i = 0; i < strlen(input); i++) {
		output[i] = input[i] ^ key[i % (sizeof(key)/sizeof(char))];
	}
}

There is a bug when do the encryption and decryption in one function above.
As the strlen(input) may be smaller than the original string length.So is the below:

void encryptDecrypt(char *input, char *output, int size) {
	char key[] = {'K', 'C', 'Q'}; //Can be any chars, and any size array
	
	int i;
	for(i = 0; i < size; i++) {
		output[i] = input[i] ^ key[i % (sizeof(key)/sizeof(char))];
	}
}