-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathHillCipher.java
310 lines (294 loc) · 13.1 KB
/
HillCipher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
------------------------------------------------ Prerequisite -----------------------------------------------------------
--> The message should be in the form of, if the is number of letters in key is even then the number of letters in
the text should also be even (similarly for odd) for smooth message transfer.
---------------------------------------------- Problem Statement --------------------------------------------------------
Given a string which needed to be encrypted based on hill cipher and decrypt it.
INPUT :- Password --> Text message
text --> Key
OUTPUT :- ZHYCGSXG --> Encrypted text
Password --> Decrypted text
-------------------------------------------------- Hill Cipher -----------------------------------------------------------
Hill cipher is a poly graphic substitution cipher based on linear algebra.Each letter is represented by a number modulo 26.
Often the simple scheme A = 0, B = 1, …, Z = 25 is used, but this is not an essential feature of the cipher. To encrypt
a message, each block of n letters (considered as an n-component vector) 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 matrix used for encryption is the cipher key, and it should be chosen randomly from the set of invertible
n × n matrices (modulo 26).
--------------------------------------------------- Algorithm -----------------------------------------------------------
1) Our key is text (2x2), Convert this key using a substitution scheme into a 2x2 key matrix
2) We will convert our plain text into vector form. Since the key matrix is 2x2, the vector must be 2x1 for matrix multiplication.
(Suppose the key matrix is 3x3, a vector will be a 3x1 matrix.)
3) Multiply the key matrix with each 2x1 plain text vector, and take the modulo of result (2x1 vectors) by 26.
Then concatenate the results.
4) Calculate the inverse of the key matrix. First, we need to find the determinant of the key matrix (must be between 0-25)
The Extended Euclidean algorithm is used to get modulo multiplicative inverse of key matrix determinant
5) we multiply the 2x1 blocks of ciphertext and the inverse of the key matrix. The resultant block after concatenation
is the plain text that we have encrypted.
--------------------------------------------------- Complexities --------------------------------------------------------
Time Complexity :- BigO(n^2) --> Traversing a 2D array message matrix.
Space Complexity :- BigO(n^2) --> A 2D array is required for storing the message.
*/
import java.util.Scanner; // Importing scanner class to get input from user.
public class HillCipher
{
public static void main(String[] args)
{
// Initializing the scanner class
Scanner sc = new Scanner(System.in);
// Reading the text message from the user which is needed to be encrypted.
System.out.print("Enter the text message = ");
String text = sc.nextLine();
// Reading the key from the user which is used to encrypt and decrypt the user message
System.out.print("Enter a key = ");
String key = sc.nextLine();
// Calling the method to encrypt the message of the usr.
encryption(text,key);
}
// Method that encrypt the user message.
private static void encryption(String text, String key)
{
// Storing the text message in temp string.
String text_message = text;
// Converting the lowercase letters to uppercase letters
text = text.toUpperCase();
key = key.toUpperCase();
// If the text length is odd making it even.
int lenChk = 0;
if (text.length() % 2 != 0)
{
text += "0";
lenChk = 1;
}
// Initializing a 2D matrix of length of key as row and text message length as column.
int[][] text2D = new int[2][text.length()];
// Initializing the iterating variables to look into the 2D array of text message.
int itr1 = 0;
int itr2 = 0;
// Converting the messages into array form.
for (int i = 0; i < text.length(); i++)
{
if (i%2 == 0)
{
text2D[0][itr1] = ((int)text.charAt(i)) - 65;
itr1++;
}
else
{
text2D[1][itr2] = ((int)text.charAt(i)) - 65;
itr2++;
}
}
// Initializing a 2D matrix of length of key as row and column.
int[][] key2D = new int[2][2];
// Initializing the iterating variable to look into the 2D array of key.
int itr3 = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
key2D[i][j] = (int)key.charAt(itr3)-65;
itr3++;
}
}
// Validating the key
// Finding determinant of key matrix
int deter = key2D[0][0] * key2D[1][1] - key2D[0][1] * key2D[1][0];
// Calling the modulo function to perform the modulo operation
deter = moduloFunction(deter);
// Calling the multiplicative inverse function to check whether the key is valid or not.
int mulInverse = multiplicativeInverse(deter);
// If the key is invalid this condition occurs,
if (mulInverse == -1)
{
System.out.println("invalid key");
System.exit(1);
}
// Empty variable to store the encrypted text.
String encryptText = "";
int itrCount = text.length() / 2;
// If text length % 2 = 0 (ie, regular length text)
if (lenChk == 0)
{
// Loop to perform encryption with the key and add it to the encryptText string.
for (int i = 0; i < itrCount; i++)
{
int temp1 = text2D[0][i] * key2D[0][0] + text2D[1][i] * key2D[0][1];
encryptText += (char)((temp1 % 26) + 65);
int temp2 = text2D[0][i] * key2D[1][0] + text2D[1][i] * key2D[1][1];
encryptText += (char)((temp2 % 26) + 65);
}
}
// If text length % 2 != 0 (ie, irregular length text)
else
{
// Loop to perform encryption with the key and add it to the encryptText string.
for (int i = 0; i < itrCount-1; i++)
{
int temp1 = text2D[0][i] * key2D[0][0] + text2D[1][i] * key2D[0][1];
encryptText += (char)((temp1 % 26) + 65);
int temp2 = text2D[0][i] * key2D[1][0] + text2D[1][i] * key2D[1][1];
encryptText += (char)((temp2 % 26) + 65);
}
}
// Printing the encrypted text.
System.out.println("Encrypted Text: " + encryptText);
// Calling the decrypt function to perform the decryption operation.
decryption(encryptText,key,text_message);
}
// Function that decrypts the encrypted text message.
private static void decryption(String encryptText, String key, String text)
{
// If the text length is odd making it even.
int lenChk = 0;
if (encryptText.length() % 2 != 0)
{
encryptText += "0";
lenChk = 1;
}
// Initializing a 2D matrix of length of key as row and encrypt message length as column.
int[][] encryptText2D = new int[2][encryptText.length()];
// Initializing the iterating variables to look into the 2D array of text message.
int itr1 = 0;
int itr2 = 0;
// Converting the encrypted message into array form.
for (int i = 0; i < encryptText.length(); i++)
{
if (i%2 == 0)
{
encryptText2D[0][itr1] = ((int)encryptText.charAt(i)) - 65;
itr1++;
}
else
{
encryptText2D[1][itr2] = ((int)encryptText.charAt(i)) - 65;
itr2++;
}
}
// Initializing a 2D matrix of length of key as row and column.
int[][] key2D = new int[2][2];
// Initializing the iterating variable to look into the 2D array of key.
int itr3 = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
key2D[i][j] = (int)key.charAt(itr3)-65;
itr3++;
}
}
// Validating the key
// Finding determinant of key matrix
int deter = key2D[0][0] * key2D[1][1] - key2D[0][1] * key2D[1][0];
// Calling the modulo function to perform the modulo operation
deter = moduloFunction(deter);
// Calling the multiplicative inverse function to check whether the key is valid or not.
int mulInverse = multiplicativeInverse(deter);
// If the key is invalid this condition occurs,
if (mulInverse == -1)
{
System.out.println("invalid key");
System.exit(1);
}
// Calculating adjugate matrix or adjoint matrix
// swapping the values
int swapTemp = key2D[0][0];
key2D[0][0] = key2D[1][1];
key2D[1][1] = swapTemp;
// Changing signs of the value
key2D[0][1] *= -1;
key2D[1][0] *= -1;
// Calling the modulo function to decrypt the message.
key2D[0][1] = moduloFunction(key2D[0][1]);
key2D[1][0] = moduloFunction(key2D[1][0]);
// Multiplying multiplicative inverse with adjoint matrix
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
key2D[i][j] *= mulInverse;
}
}
// Initializing the key with the modulo of the character value.
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
key2D[i][j] = moduloFunction(key2D[i][j]);
}
}
// Initializing an empty string variable to store the decryptText message.
String decryptText = "";
int itrCount = encryptText.length() / 2;
// If text length % 2 = 0 (ie, regular length text)
if (lenChk == 0)
{
// Loop to perform encryption with the key and add it to the encryptText string.
for (int i = 0; i < itrCount; i++)
{
int temp1 = encryptText2D[0][i] * key2D[0][0] + encryptText2D[1][i] * key2D[0][1];
decryptText += (char)((temp1 % 26) + 65);
int temp2 = encryptText2D[0][i] * key2D[1][0] + encryptText2D[1][i] * key2D[1][1];
decryptText += (char)((temp2 % 26) + 65);
}
}
// If text length % 2 != 0 (ie, irregular length text)
else
{
// Loop to perform encryption with the key and add it to the encryptText string.
for (int i = 0; i < itrCount-1; i++)
{
int temp1 = encryptText2D[0][i] * key2D[0][0] + encryptText2D[1][i] * key2D[0][1];
decryptText += (char)((temp1 % 26) + 65);
int temp2 = encryptText2D[0][i] * key2D[1][0] + encryptText2D[1][i] * key2D[1][1];
decryptText += (char)((temp2 % 26) + 65);
}
}
// Using a string buffer class to convert the text into correct case.
StringBuffer decrypted = new StringBuffer(decryptText);
// Loop to iterate to change the uppercase letter to lower case letter.
for(int i=0;i<decryptText.length();i++)
{
// Checking it is lower case or not in the message
if(Character.isLowerCase(text.charAt(i)))
{
// Converting the uppercase letter to lower case using string builder.
decrypted.setCharAt(i, Character.toLowerCase(decryptText.charAt(i)));
}
}
// Printing the decrypted message.
System.out.println("Decrypted Text: " + decrypted);
}
// Function that performs modulo operation.
private static int moduloFunction(int a)
{
// Storing modulo of the value.
int result = a % 26;
// In case the modulo is negative, we are making it as positive by this condition.
if (result < 0)
{
result += 26;
}
// Returning the modulo value which is used to find the multiplicative inverse.
return result;
}
// unction that say's that the key is valid or not using multiplicative inverse.
private static int multiplicativeInverse(int deter)
{
// Initializing an iterating variable to store the value of multiplicative inverse
int mulInverse;
for (int i = 0; i < 26; i++)
{
int tempInv = deter * i;
// If the modulo of the key value is 1 then the key is suitable for us.
if (moduloFunction(tempInv) == 1)
{
mulInverse = i;
// Returning the multiplicative inverse
return mulInverse;
}
}
// In case key is not suitable for us then we will return -1.
return -1;
}
}