// Lauryn Landkrohn
// CIS 3362
// Fall 2019

import java.util.*;

class HillCipher
{
	static int[][] keyMatrix = {
		{16, 4, 5, 22},
		{11, 14, 4, 24},
		{5, 7, 2, 25},
		{15, 22, 19, 19}
	};

	static String plaintext = "jackandjillwentdownahilltofetchapailofwaterx";

	static void encrypt()
	{
		int blocksize = keyMatrix.length;
		int plaintextNum[][] = new int[plaintext.length() / blocksize][blocksize];
		int cipherMatrix[][] = new int[plaintext.length() /blocksize][blocksize];
		String cipherText = "";

		for(int i = 0; i < plaintext.length(); i++)
		{
			plaintextNum[i/blocksize][i%blocksize] = plaintext.charAt(i) - 'a';
		}

		// there are plaintext length / blocksize main blocks with j letters
		for(int i = 0; i < plaintextNum.length; i++)
		{
			for(int j = 0; j < plaintextNum[i].length; j++)
			{
				// the key is iterated over horizontally instead of vertically
				for(int x = 0; x < keyMatrix[j].length; x++)
				{
					cipherMatrix[i][j] += keyMatrix[j][x] * plaintextNum[i][x];
				}
				// stores the number mod 26
				cipherMatrix[i][j] = cipherMatrix[i][j] % 26;
				// converts the letter to english and stores in ciphertext
				cipherText += (char)(cipherMatrix[i][j] + 'a');
			}
		}

		// prints ciphertext
		System.out.println(cipherText);
	}

	public static void main(String[] args)
	{
		encrypt();
	}
}
