public class Lab04 { private static final String eKey = "foobarbaz"; private static final String dKey = "aoq.geasl"; public static void main(String[] args) { /* Converts the encryption key into a matrix */ int[][] eMatrix = keyToMatrix(eKey); /* Converts the decryption key into a matrix */ int[][] dMatrix = keyToMatrix(dKey); /* Our plaintext to be encrypted */ String text = "hello, test."; String cipher = transform(eMatrix, text); /* The below should be "nuvfxprhkjn " */ System.out.println("\"" + cipher + "\""); /* The below should be "hello, test." */ System.out.println("\"" + transform(dMatrix, cipher) + "\""); } /** * Converts a character into its integer equivalent for the Hill cipher. *
* This means a = 1; b = 2; c = 3; ... ; z = 25; SPACE=26, .=27, COMMA=28. * * @param c The character which is being converted. */ public static int charToNumber(char c) { switch (c) { case ' ': return 26; case '.': return 27; case ',': return 28; default: return c - 'a'; } } /** * Performs a matrix by vector multiplication modulo 29. * * @param matrix The matrix being multiplied * @param vector The vector to multiply by */ public static int[] multiply(int[][] matrix, int[] vector) { /* TODO 2: Write this method. */ } /** * Converts text to an array. The text must consist only of lowercase * letters. The conversion is a=0, b=1, c=2, etc. * * @param text The text; must consist only of lowercase letters. */ public static int[] stringToArray(String text) { /* TODO 3: Write this method. */ } /** * Converts an array given from stringToArray back to a String. * * @param array The array to convert. */ public static String arrayToString(int[] array) { /* The accumulating variable for the end result. */ String r = ""; /* TODO 4: Write the loop to convert the array back to a String. You * should store the result in r. */ return r; } /** * Encrypts some text with the Hill cipher. The text must consist of only * lowercase letters and must be a multiple of the length of the key matrix. * The encryption encrypts the text in blocks and returns the concatenation * of the results. * * @param key The key matrix. Must be be of dimensions N by N. * @param text The text to encrypt. Must be of length k*N, k is an integer. */ public static String transform(int[][] key, String text) { // The number of blocks of text that you'll have to encrypt. int blocks = text.length() / key.length; // The size of each block int size = key.length; // The variable that accumulates the result from each block String result = ""; /* TODO 5: Write the code to encrypt each block. */ return result; } /** * Transforms a text key to a matrix key. The text key must consist only of * lowercase letters. The conversion is a=0, b=1, c=2, etc. * * @param key The text key; must consist only of lowercase letters. */ public static int[][] keyToMatrix(String key) { // The length of one side of the matrix. int length = (int)Math.sqrt(key.length()); /* TODO 6: Write the loop(s) to convert the key into a 2D matrix. */ } }