Hill Cipher
Hill Cipher
ArrayList;
import java.util.Scanner;
double sq = Math.sqrt(key.length());
int k = 0;
k++;
return keyMatrix;
if(det == 0) {
throw new java.lang.Error("Det equals to zero, invalid key matrix!");
// This method checks if the reverse key matrix is valid (matrix mod26 = (1,0,0,1)
// Find the product matrix of key matrix times reverse key matrix
int factor;
// factor*det = 1 mod 26
if((detmod26 * factor) % 26 == 1)
{
break;
// Calculate the reverse key matrix elements using the factor found
return reverseMatrix;
int i;
System.out.print(label);
if(i+2 <phrase.size()) {
System.out.print("-");
System.out.println();
int i;
int[][] keyMatrix;
phrase = phrase.replaceAll("[^a-zA-Z]","").toUpperCase();
if(phrase.length() % 2 == 1) {
phrase += "Q";
keyMatrix = getKeyMatrix();
isValidMatrix(keyMatrix);
// Find the product per pair of the phrase with the key matrix modulo 26
phraseEncoded.add(alphaZero ? x : (x == 0 ? 26 : x ));
phraseEncoded.add(alphaZero ? y : (y == 0 ? 26 : y ));
phrase = phrase.replaceAll("[^a-zA-Z]","").toUpperCase();
keyMatrix = getKeyMatrix();
isValidMatrix(keyMatrix);
revKeyMatrix = reverseMatrix(keyMatrix);
isValidReverseMatrix(keyMatrix, revKeyMatrix);
// Find the product per pair of the phrase with the reverse key matrix modulo 26
//main method
byte[] p;
System.out.println("-------------------------");
System.out.println();
System.out.println();
opt = sc.nextLine();
switch (opt)
case "1":
phrase = sc.nextLine();
encrypt(phrase, true);
break;
case "2":
phrase = sc.nextLine();
decrypt(phrase, true);
break;
case "3":
encrypt(phrase, false);
break;
case "4":
phrase = sc.nextLine();
decrypt(phrase, false);
break;