12
12
*/
13
13
public class HillCipher {
14
14
15
- static Scanner in = new Scanner (System .in );
15
+ static Scanner userInput = new Scanner (System .in );
16
16
17
17
/* Following function encrypts the message
18
18
*/
19
19
static void encrypt (String message ) {
20
20
message = message .toUpperCase ();
21
21
// Get key matrix
22
22
System .out .println ("Enter key matrix size" );
23
- int n = in .nextInt ();
23
+ int matrixSize = userInput .nextInt ();
24
24
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 ();
29
29
}
30
30
}
31
31
//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 );
36
33
37
- int [][] messageVector = new int [n ][1 ];
34
+ int [][] messageVector = new int [matrixSize ][1 ];
38
35
String CipherText = "" ;
39
- int cipherMatrix [][] = new int [n ][1 ];
36
+ int cipherMatrix [][] = new int [matrixSize ][1 ];
40
37
int j = 0 ;
41
38
while (j < message .length ()) {
42
- for (int i = 0 ; i < n ; i ++) {
39
+ for (int i = 0 ; i < matrixSize ; i ++) {
43
40
if (j >= message .length ()) {
44
41
messageVector [i ][0 ] = 23 ;
45
42
} else {
@@ -49,16 +46,16 @@ static void encrypt(String message) {
49
46
j ++;
50
47
}
51
48
int x , i ;
52
- for (i = 0 ; i < n ; i ++) {
49
+ for (i = 0 ; i < matrixSize ; i ++) {
53
50
cipherMatrix [i ][0 ] = 0 ;
54
51
55
- for (x = 0 ; x < n ; x ++) {
52
+ for (x = 0 ; x < matrixSize ; x ++) {
56
53
cipherMatrix [i ][0 ] += keyMatrix [i ][x ] * messageVector [x ][0 ];
57
54
}
58
55
System .out .println (cipherMatrix [i ][0 ]);
59
56
cipherMatrix [i ][0 ] = cipherMatrix [i ][0 ] % 26 ;
60
57
}
61
- for (i = 0 ; i < n ; i ++) {
58
+ for (i = 0 ; i < matrixSize ; i ++) {
62
59
CipherText += (char ) (cipherMatrix [i ][0 ] + 65 );
63
60
}
64
61
}
@@ -70,19 +67,17 @@ static void decrypt(String message) {
70
67
message = message .toUpperCase ();
71
68
// Get key matrix
72
69
System .out .println ("Enter key matrix size" );
73
- int n = in .nextInt ();
70
+ int n = userInput .nextInt ();
74
71
System .out .println ("Enter inverseKey/decryptionKey matrix " );
75
72
int keyMatrix [][] = new int [n ][n ];
76
73
for (int i = 0 ; i < n ; i ++) {
77
74
for (int j = 0 ; j < n ; j ++) {
78
- keyMatrix [i ][j ] = in .nextInt ();
75
+ keyMatrix [i ][j ] = userInput .nextInt ();
79
76
}
80
77
}
81
78
//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
+
86
81
//solving for the required plaintext message
87
82
int [][] messageVector = new int [n ][1 ];
88
83
String PlainText = "" ;
@@ -145,12 +140,12 @@ public static int determinant(int a[][], int n) {
145
140
}
146
141
147
142
// Function to implement Hill Cipher
148
- static void hillcipher (String message ) {
143
+ static void hillCipher (String message ) {
149
144
message .toUpperCase ();
150
145
System .out .println ("What do you want to process from the message?" );
151
146
System .out .println ("Press 1: To Encrypt" );
152
147
System .out .println ("Press 2: To Decrypt" );
153
- short sc = in .nextShort ();
148
+ short sc = userInput .nextShort ();
154
149
if (sc == 1 ) {
155
150
encrypt (message );
156
151
} else if (sc == 2 ) {
@@ -160,11 +155,18 @@ static void hillcipher(String message) {
160
155
}
161
156
}
162
157
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
+
163
165
// Driver code
164
166
public static void main (String [] args ) {
165
167
// Get the message to be encrypted
166
168
System .out .println ("Enter message" );
167
- String message = in .nextLine ();
168
- hillcipher (message );
169
+ String message = userInput .nextLine ();
170
+ hillCipher (message );
169
171
}
170
- }
172
+ }
0 commit comments