-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeySchedule.java
More file actions
40 lines (32 loc) · 1.18 KB
/
KeySchedule.java
File metadata and controls
40 lines (32 loc) · 1.18 KB
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
import java.util.*;
public class KeySchedule {
// Circular left shift by 1 bit
public static String shiftIt(String input) {
return input.substring(1) + input.charAt(0);
}
// for encryption:
public static Queue<String> generateKeys(String inputKey) {
Queue<String> keys = new LinkedList<>();
String binaryKey = Main.toBinary(inputKey).substring(0, 56); // force exactly 56 bits
String C = binaryKey.substring(0, 28);
String D = binaryKey.substring(28);
// generate for loop for 10 subkeys:
for (int i = 0; i < 10; i++) {
C = shiftIt(C);
D = shiftIt(D);
String combined = C + D; // 56 bits
String subkey = combined.substring(0, 32); // first 32 bits
keys.add(subkey);
}
return keys;
}
// for decryption:
public static Stack<String> generateKeysReverse(String inputKey) {
Stack<String> reversed = new Stack<>();
List<String> forward = new ArrayList<>(generateKeys(inputKey));
for (int i = forward.size() - 1; i >= 0; i--) {
reversed.push(forward.get(i));
}
return reversed;
}
}