Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

there is a bug with the c version #12

Open
hjw21century opened this issue Dec 18, 2017 · 0 comments
Open

there is a bug with the c version #12

hjw21century opened this issue Dec 18, 2017 · 0 comments

Comments

@hjw21century
Copy link

hjw21century commented Dec 18, 2017

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))];
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant