// Jade Bowyer
// 9/7/2020
// Helper code for CIS 3362 Homework 2 Problem 2

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

//takes the filename as a command line argument
int main(int argc, char * argv[]) {
	int i, j, length, n;

	//this file should contain the list of possible keywords (lowercase)
	FILE * ifp = fopen(argv[1], "r");

	//confirming the filename is valid
	if (ifp == NULL) {
		printf("File not found.\n");
		return 1;
	}

	//user inputs the ciphertext
	char ciphertext[1000], plaintext[1000];
	printf("Enter the ciphertext:\n");
	scanf("%s", ciphertext);
	length = strlen(ciphertext);

	char key[50];
	for (i = 0; i < 1000; i++) {
		//grabs a keyword from the file
		fscanf(ifp, "%s", key);
		n = strlen(key);

		//decrypts the ciphertext using the keyword
		for (j = 0; j < length; j++) {
			plaintext[j] = (ciphertext[j] - key[j%n] + 26)%26 + 'a';
		}
		plaintext[j]='\0';

		//prints the plaintext if it contains the substring "mate"
		if (strstr(plaintext, "mate") != NULL)
			printf("\nKey: %s\nPlaintext: %s\n", key, plaintext);
	}

	fclose(ifp);

	return 0;
}
