#include <stdio.h>
#include <string.h>

int main() {
	char msg[300];
	FILE * ifp = fopen("keylist.txt","r");
	int i, j, length;

	printf("Please enter the message:\n");
	scanf("%s",msg);

	length = strlen(msg);

	//looking at each possible key
	for (i = 0; i < 1000; i++) {
		char key[20], txt[300];
		int flag = 0;
		
		fscanf(ifp, "%s", key);

		int keysize = strlen(key);

		//decrypting each character
		for (j = 0; j < length; j++) {
			int cval = msg[j] - key[j%keysize];
			if (cval < 0) cval += 26;
			txt[j] = cval + 97;

			//checking if "fast" is present
			if (!flag && txt[j] == 't') {
				if (j >= 3 && txt[j-3] == 'f'
					&& txt[j-2] == 'a' && txt[j-1] == 's')
					flag = 1;
			}
		}

		//printing valid results
		if (flag)
			printf("\nKey: %s\n%s\n\n", key, txt);
	}

	fclose(ifp);

	return 0;
}
