-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cryptographic_System.java
486 lines (419 loc) · 13.8 KB
/
Cryptographic_System.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import java.util.Scanner;
import java.lang.Math;
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.io.*;
class Encrypt{
public String removeWhiteSpace(String plaintext)
{
plaintext = plaintext.replaceAll("\\s", "");
return plaintext;
}
public String removeRepeatedCharacter(String input)
{
String output = new String();
int c = 2;
output = output + input.charAt(0);
for (int i = 0; i < input.length(); i++)
{
for (int j = 0; j < output.length(); j++)
{
if (input.charAt(i) == output.charAt(j))
{
c = 1;
break;
}
else
{
c = 0;
}
}
if(c == 0)
{
output = output + input.charAt(i);
}
}
return output;
}
public String playFair(String plaintext)
{
String ciphertext = new String();
Scanner s = new Scanner(System.in);
String key;
int i,j,t,c = 2;//loop and conditional variable
int x,y,z;//temp variables
char en_block[][] = new char[5][5];//key block
char bn_block[][] = {{'a','b','c','d','e'},{'f','g','h','i','k'},{'l','m','n','o','p'},{'q','r','s','t','u'},{'v','w','x','y','z'}};
//abcd block
char cipher[] = new char[2];
plaintext = removeWhiteSpace(plaintext);
plaintext = plaintext.toLowerCase();
plaintext = plaintext.replaceAll("j","i");
System.out.println("Enter the key: ");
key = s.nextLine();
key = removeWhiteSpace(key);
key = key.toLowerCase();
key = removeRepeatedCharacter(key);
//inserting key in keyblock
for(i = 0;i < key.length();i++)
{
t = i % 5;
j = i / 5;
en_block[j][t] = key.charAt(i);
}
//preparing key block after inserting key in block
z = key.length();
y = z % 5;
x = z / 5;
for(i = 0;i < 5;i++)
{
for(j = 0;j < 5;j++)
{
c = 1;
for(t = 0;t < z;t++)
{
if(bn_block[i][j] == key.charAt(t))
{
c = 0;
}
}
if(c == 1)
{
en_block[x][y] = bn_block[i][j];
y++;
x = x + (y / 5);
y = y % 5;
}
}
}
int a = 6,b = 6;//rows and cols for first char initializatin 6 is just for reference
int e = 6,d = 6;//rows and cols for second char
for(i = 0;i < plaintext.length();i += 2)
{
cipher[0] = plaintext.charAt(i);
if(i+1 >= plaintext.length())
{
plaintext += 'x';
}
cipher[1] = plaintext.charAt(i+1);
//taking dummy character for repeating characters
if(cipher[0] == cipher[1] && cipher[0] != x)
{
cipher[1] = 'x';
i--;
}
for(x = 0;x < 5;x++)
{
for(y = 0;y < 5;y++)
{
if(cipher[0] == en_block[x][y])
{
a = x;//rows of 1 ch
b = y;//cols of 1 ch
}
}
}
for(x = 0;x < 5;x++)
{
for(y = 0;y < 5;y++)
{
if(cipher[1] == en_block[x][y])
{
d = x;//rows of 1 ch
e = y;//cols of 1 ch
}
}
}
//same row
if(d == a)
{
b = (b + 1) % 5;
e = (e + 1) % 5;
//taking next char in same row in cyclic manner
cipher[0] = en_block[a][b];
cipher[1] = en_block[d][e];
}
//same column
else if(e == b)
{
a = (a + 1) % 5;
d = (d + 1) % 5;
//taking next char in same column in cyclic manner
cipher[0] = en_block[a][b];
cipher[1] = en_block[d][e];
}
else
{
//taking char of same row and column of second char in keyblock
cipher[0] = en_block[a][e];
cipher[1] = en_block[d][b];
}
//inserting pair of ciphered char into string
ciphertext = ciphertext +cipher[0];
ciphertext = ciphertext +cipher[1];
}
s.close();
return ciphertext;
}
}
class Decrypt
{
public String removeWhiteSpace(String plaintext)
{
plaintext = plaintext.replaceAll("\\s", "");
return plaintext;
}
public String removeRepeatedCharacter(String input)
{
String output = new String();
int c = 2;
output = output + input.charAt(0);
for (int i = 0; i < input.length(); i++)
{
for (int j = 0; j < output.length(); j++)
{
if (input.charAt(i) == output.charAt(j))
{
c = 1;
break;
}
else
{
c = 0;
}
}
if(c == 0)
{
output = output + input.charAt(i);
}
}
return output;
}
public String playFair(String ciphertext)
{
String plaintext = new String();
Scanner s = new Scanner(System.in);
String key;
int i,j,t,c = 2;//loop and conditional variable
int x,y,z;//temp variables
char en_block[][] = new char[5][5];//key block
char bn_block[][] = {{'a','b','c','d','e'},{'f','g','h','i','k'},{'l','m','n','o','p'},{'q','r','s','t','u'},{'v','w','x','y','z'}};
//abcd block
char plain[] = new char[2];
System.out.println("Enter the key: ");
key = s.nextLine();
key = removeWhiteSpace(key);
key = key.toLowerCase();
key = removeRepeatedCharacter(key);
//inserting key in keyblock
for(i = 0;i < key.length();i++)
{
t = i % 5;
j = i / 5;
en_block[j][t] = key.charAt(i);
}
//preparing key block after inserting key in block
z = key.length();
y = z % 5;
x = z / 5;
for(i = 0;i < 5;i++)
{
for(j = 0;j < 5;j++)
{
c = 1;
for(t = 0;t < z;t++)
{
if(bn_block[i][j] == key.charAt(t))
{
c = 0;
}
}
if(c == 1)
{
en_block[x][y] = bn_block[i][j];
y++;
x = x + (y / 5);
y = y % 5;
}
}
}
int a = 6,b = 6;//rows and cols for first char initializatin 6 is just for reference
int e = 6,d = 6;//rows and cols for second char
for(i = 0;i < ciphertext.length();i += 2)
{
plain[0] = ciphertext.charAt(i);
plain[1] = ciphertext.charAt(i+1);
for(x = 0;x < 5;x++)
{
for(y = 0;y < 5;y++)
{
if(plain[0] == en_block[x][y])
{
a = x;//rows of 1 ch
b = y;//cols of 1 ch
}
}
}
for(x = 0;x < 5;x++)
{
for(y = 0;y < 5;y++)
{
if(plain[1] == en_block[x][y])
{
d = x;//rows of 1 ch
e = y;//cols of 1 ch
}
}
}
//same row
if(d == a)
{
b = (b - 1) % 5;
e = (e - 1) % 5;
if(b < 0)
{
b += 5;
}
if(e < 0)
{
e += 5;
}
//taking next char in same row in cyclic manner
plain[0] = en_block[a][b];
plain[1] = en_block[d][e];
}
//same column
else if(e == b)
{
a = (a - 1) % 5;
d = (d - 1) % 5;
if(a < 0)
{
a += 5;
}
if(d < 0)
{
d += 5;
}
//taking next char in same column in cyclic manner
plain[0] = en_block[a][b];
plain[1] = en_block[d][e];
}
else
{
//taking char of same row and column of second char in keyblock
plain[0] = en_block[a][e];
plain[1] = en_block[d][b];
}
//inserting pair of ciphered char into string
plaintext = plaintext + plain[0];
plaintext = plaintext + plain[1];
}
s.close();
return plaintext;
}
}
class Cryptographic_System {
public static byte[] getSHA(String input) throws NoSuchAlgorithmException {
// Static getInstance method is called with hashing SHA
MessageDigest md = MessageDigest.getInstance("SHA-256");
// digest() method called
// to calculate message digest of an input
// and return array of byte
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash) {
// Convert byte array into signum representation
BigInteger number = new BigInteger(1, hash);
// Convert message digest into hex value
StringBuilder hexString = new StringBuilder(number.toString(16));
// Pad with leading zeros
while (hexString.length() < 32) {
hexString.insert(0, '0');
}
return hexString.toString();
}
public static void main(String args[]) {
String plaintext = new String();
String ciphertext = new String();
int option;
int a;// switching variables
Encrypt e = new Encrypt();// Objects of diffrent class
Decrypt d = new Decrypt();
Scanner s = new Scanner(System.in);
String s3 = "Murtaza";//required user name
System.out.print("Enter UserName: ");
String s2 = s.nextLine();
while(s2.equals(s3) != true)//loop until valid username is printed
{
System.out.println("!!Enter valid UserName!!");
s2 = s.nextLine();
}
File pass = new File("hashValue.txt");//opening file which has hash value of password
try {
Scanner sn = new Scanner(pass);
s3 = sn.nextLine();//scanning hash value of password and storing in string
sn.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
System.out.print("\nEnter Password: ");
String s1 = s.nextLine();//scanning password
try{
s1 = toHexString(getSHA(s1));//converting password to hash value
}
catch (NoSuchAlgorithmException l) {
System.out.println("Exception thrown for incorrect algorithm: " + l);
}
if(s1.equals(s3) != true)
{
System.out.println("!!Entered Password is wrong!!");
}
while(s1.equals(s3) != true)
{
System.out.println("Enter Password:");
s1 = s.nextLine();//scanning password
try{
s1 = toHexString(getSHA(s1));//converting password to hash value
}
catch (NoSuchAlgorithmException l) {
System.out.println("Exception thrown for incorrect algorithm: " + l);
}
if(s1.equals(s3) != true)
{
System.out.println("!!Entered Password is wrong!!");
}
}
System.out.println("\nSelect the algortihm:");
System.out.println("1 - PlayFair Cipher");
option = s.nextInt();
switch(option){
case 1:
System.out.println("\nChoose Option:");
System.out.println("1 - Encrypt Message");
System.out.println("2 - Decrypt Message");
a = s.nextInt();
switch(a)
{
case 1:
System.out.println("\nEnter your message to encrypt:");
plaintext = s.nextLine();
plaintext = s.nextLine();
ciphertext = e.playFair(plaintext);
System.out.println(ciphertext);
break;
case 2:
System.out.println("Enter your message to decrypt:");
ciphertext = s.nextLine();
ciphertext = s.nextLine();
plaintext = d.playFair(ciphertext);
System.out.println(plaintext);
break;
}
break;
}
s.close();
}
}