Skip to content

there is a bug with the c version #12

@hjw21century

Description

@hjw21century

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))];
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions