Skip to content

Commit 1d5d672

Browse files
Rushipatel0995Rushi
and
Rushi
authored
Refactor to remove code smells (TheAlgorithms#2982)
Co-authored-by: Rushi <rs397441@dal.ca>
1 parent 4da2742 commit 1d5d672

File tree

4 files changed

+374
-342
lines changed

4 files changed

+374
-342
lines changed

src/main/java/com/thealgorithms/ciphers/HillCipher.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,31 @@
1212
*/
1313
public class HillCipher {
1414

15-
static Scanner in = new Scanner(System.in);
15+
static Scanner userInput = new Scanner(System.in);
1616

1717
/* Following function encrypts the message
1818
*/
1919
static void encrypt(String message) {
2020
message = message.toUpperCase();
2121
// Get key matrix
2222
System.out.println("Enter key matrix size");
23-
int n = in.nextInt();
23+
int matrixSize = userInput.nextInt();
2424
System.out.println("Enter Key/encryptionKey matrix ");
25-
int keyMatrix[][] = new int[n][n];
26-
for (int i = 0; i < n; i++) {
27-
for (int j = 0; j < n; j++) {
28-
keyMatrix[i][j] = in.nextInt();
25+
int keyMatrix[][] = new int[matrixSize][matrixSize];
26+
for (int i = 0; i < matrixSize; i++) {
27+
for (int j = 0; j < matrixSize; j++) {
28+
keyMatrix[i][j] = userInput.nextInt();
2929
}
3030
}
3131
//check if det = 0
32-
if (determinant(keyMatrix, n) % 26 == 0) {
33-
System.out.println("Invalid key, as determinant = 0. Program Terminated");
34-
return;
35-
}
32+
validateDeterminant(keyMatrix,matrixSize);
3633

37-
int[][] messageVector = new int[n][1];
34+
int[][] messageVector = new int[matrixSize][1];
3835
String CipherText = "";
39-
int cipherMatrix[][] = new int[n][1];
36+
int cipherMatrix[][] = new int[matrixSize][1];
4037
int j = 0;
4138
while (j < message.length()) {
42-
for (int i = 0; i < n; i++) {
39+
for (int i = 0; i < matrixSize; i++) {
4340
if (j >= message.length()) {
4441
messageVector[i][0] = 23;
4542
} else {
@@ -49,16 +46,16 @@ static void encrypt(String message) {
4946
j++;
5047
}
5148
int x, i;
52-
for (i = 0; i < n; i++) {
49+
for (i = 0; i < matrixSize; i++) {
5350
cipherMatrix[i][0] = 0;
5451

55-
for (x = 0; x < n; x++) {
52+
for (x = 0; x < matrixSize; x++) {
5653
cipherMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0];
5754
}
5855
System.out.println(cipherMatrix[i][0]);
5956
cipherMatrix[i][0] = cipherMatrix[i][0] % 26;
6057
}
61-
for (i = 0; i < n; i++) {
58+
for (i = 0; i < matrixSize; i++) {
6259
CipherText += (char) (cipherMatrix[i][0] + 65);
6360
}
6461
}
@@ -70,19 +67,17 @@ static void decrypt(String message) {
7067
message = message.toUpperCase();
7168
// Get key matrix
7269
System.out.println("Enter key matrix size");
73-
int n = in.nextInt();
70+
int n = userInput.nextInt();
7471
System.out.println("Enter inverseKey/decryptionKey matrix ");
7572
int keyMatrix[][] = new int[n][n];
7673
for (int i = 0; i < n; i++) {
7774
for (int j = 0; j < n; j++) {
78-
keyMatrix[i][j] = in.nextInt();
75+
keyMatrix[i][j] = userInput.nextInt();
7976
}
8077
}
8178
//check if det = 0
82-
if (determinant(keyMatrix, n) % 26 == 0) {
83-
System.out.println("Invalid key, as determinant = 0. Program Terminated");
84-
return;
85-
}
79+
validateDeterminant(keyMatrix,n);
80+
8681
//solving for the required plaintext message
8782
int[][] messageVector = new int[n][1];
8883
String PlainText = "";
@@ -145,12 +140,12 @@ public static int determinant(int a[][], int n) {
145140
}
146141

147142
// Function to implement Hill Cipher
148-
static void hillcipher(String message) {
143+
static void hillCipher(String message) {
149144
message.toUpperCase();
150145
System.out.println("What do you want to process from the message?");
151146
System.out.println("Press 1: To Encrypt");
152147
System.out.println("Press 2: To Decrypt");
153-
short sc = in.nextShort();
148+
short sc = userInput.nextShort();
154149
if (sc == 1) {
155150
encrypt(message);
156151
} else if (sc == 2) {
@@ -160,11 +155,18 @@ static void hillcipher(String message) {
160155
}
161156
}
162157

158+
static void validateDeterminant(int[][] keyMatrix, int n){
159+
if (determinant(keyMatrix, n) % 26 == 0) {
160+
System.out.println("Invalid key, as determinant = 0. Program Terminated");
161+
return;
162+
}
163+
}
164+
163165
// Driver code
164166
public static void main(String[] args) {
165167
// Get the message to be encrypted
166168
System.out.println("Enter message");
167-
String message = in.nextLine();
168-
hillcipher(message);
169+
String message = userInput.nextLine();
170+
hillCipher(message);
169171
}
170-
}
172+
}

0 commit comments

Comments
 (0)