-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDES.java
79 lines (59 loc) · 1.92 KB
/
DES.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
import javax.crypto.Cipher;
import Ciphers.DES_P.*;
import Ciphers.DES_P.HexEntries;
public class DES {
public static String CryptedStr[];
public static String Cryption(String input16[], int cryption, String... keys) {
// input is array of length 16 plain hexadecimal strings
String key;
String finalStr[] = new String[input16.length];
CryptedStr = new String[input16.length];
if (cryption == 0) {
// key is generated in hexadecimal of length 16
key = Ciphers.DES_P.KeyGenerator.getKey(16);
System.out.println("\nYour key is : ");
System.out.println("\t" + key);
CipherIT.key = key;
} else {
key = keys[0];
}
MultiThread A[] = new MultiThread[input16.length];
ThreadGroup Group = new ThreadGroup("CryptionParent");
try {
for (int i = 0; i < input16.length; i++) {
A[i] = new MultiThread(input16[i], i, key, Group, cryption);
A[i].start();
}
do {
} while (Group.activeCount() > 0);
} catch (Exception e) {
System.out.println("Main interrupted");
}
// returns hexadecimal
return String.join("", CryptedStr);
}
}
class MultiThread extends Thread {
int index;
String data;
String key;
int cryption;
MultiThread(String a, int i, String k, ThreadGroup G, int C) {
super(G, (a + i));
data = a;
index = i;
key = k;
cryption = C;
}
public void run() {
Ciphers.DES_P.DES cipher = new Ciphers.DES_P.DES();
switch (cryption) {
case 0:
DES.CryptedStr[index] = cipher.encrypt(data, key);
break;
case 1:
DES.CryptedStr[index] = cipher.decrypt(data, key);
break;
}
}
}