mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
@@ -4,10 +4,11 @@ import java.util.Scanner;
|
||||
|
||||
/*
|
||||
* Java Implementation of Hill Cipher
|
||||
* Hill cipher is a polyalphabetic substitution cipher. Each letter is represented by a number belonging to the set Z26 where A=0 , B=1, ..... Z=25.
|
||||
* To encrypt a message, each block of n letters (since matrix size is n x n) is multiplied by an invertible n × n matrix, against modulus 26.
|
||||
* To decrypt the message, each block is multiplied by the inverse of the matrix used for encryption.
|
||||
* The cipher key and plaintext/ciphertext are user inputs.
|
||||
* Hill cipher is a polyalphabetic substitution cipher. Each letter is represented by a number
|
||||
* belonging to the set Z26 where A=0 , B=1, ..... Z=25. To encrypt a message, each block of n
|
||||
* letters (since matrix size is n x n) is multiplied by an invertible n × n matrix, against
|
||||
* modulus 26. To decrypt the message, each block is multiplied by the inverse of the matrix used
|
||||
* for encryption. The cipher key and plaintext/ciphertext are user inputs.
|
||||
* @author Ojasva Jain
|
||||
*/
|
||||
public class HillCipher {
|
||||
@@ -28,7 +29,7 @@ public class HillCipher {
|
||||
keyMatrix[i][j] = userInput.nextInt();
|
||||
}
|
||||
}
|
||||
//check if det = 0
|
||||
// check if det = 0
|
||||
validateDeterminant(keyMatrix, matrixSize);
|
||||
|
||||
int[][] messageVector = new int[matrixSize][1];
|
||||
@@ -62,7 +63,7 @@ public class HillCipher {
|
||||
System.out.println("Ciphertext: " + CipherText);
|
||||
}
|
||||
|
||||
//Following function decrypts a message
|
||||
// Following function decrypts a message
|
||||
static void decrypt(String message) {
|
||||
message = message.toUpperCase();
|
||||
// Get key matrix
|
||||
@@ -75,10 +76,10 @@ public class HillCipher {
|
||||
keyMatrix[i][j] = userInput.nextInt();
|
||||
}
|
||||
}
|
||||
//check if det = 0
|
||||
// check if det = 0
|
||||
validateDeterminant(keyMatrix, n);
|
||||
|
||||
//solving for the required plaintext message
|
||||
// solving for the required plaintext message
|
||||
int[][] messageVector = new int[n][1];
|
||||
String PlainText = "";
|
||||
int[][] plainMatrix = new int[n][1];
|
||||
@@ -157,9 +158,7 @@ public class HillCipher {
|
||||
|
||||
static void validateDeterminant(int[][] keyMatrix, int n) {
|
||||
if (determinant(keyMatrix, n) % 26 == 0) {
|
||||
System.out.println(
|
||||
"Invalid key, as determinant = 0. Program Terminated"
|
||||
);
|
||||
System.out.println("Invalid key, as determinant = 0. Program Terminated");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user