-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipher.java
More file actions
111 lines (85 loc) · 3.97 KB
/
Cipher.java
File metadata and controls
111 lines (85 loc) · 3.97 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
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
import java.util.*;
public class Cipher {
//encrypt class
public static String encrypt(String binaryInput, String inputKey) {
Queue<String> subkeys = KeySchedule.generateKeys(inputKey);
StringBuilder encrypted = new StringBuilder();
for (int i = 0; i < binaryInput.length(); i += 64) {
String block = binaryInput.substring(i, i + 64);
encrypted.append(encryptBlock(block, new LinkedList<>(subkeys))); // pass a fresh copy each time
}
return encrypted.toString();
}
// decrypt class
public static String decrypt(String binaryInput, String inputKey) {
StringBuilder decrypted = new StringBuilder();
for (int i = 0; i < binaryInput.length(); i += 64) {
Stack<String> subkeys = KeySchedule.generateKeysReverse(inputKey);
String block = binaryInput.substring(i, i + 64);
String correctedBlock = block.substring(32) + block.substring(0, 32);
decrypted.append(decryptBlock(correctedBlock, subkeys));
}
return decrypted.toString();
}
// encrypt logic:
public static String encryptBlock(String block, Queue<String> subkeys) {
String L = block.substring(0, 32);
String R = block.substring(32);
for (int i = 0; i < 10; i++) {
String temp = R;
R = FeistelFunctions.xorIt(L, FeistelFunctions.functionF(R, subkeys.poll()));
L = temp;
}
return R + L;
}
// decrypt logic:
public static String decryptBlock(String block, Stack<String> subkeys) {
String L = block.substring(0, 32);
String R = block.substring(32);
for (int i = 0; i < 10; i++) {
String temp = R;
R = FeistelFunctions.xorIt(L, FeistelFunctions.functionF(R, subkeys.pop()));
L = temp;
}
return R + L;
}
// runTests method for test cases:
public static void runTests() {
// set strings for keyAll1s and keyAll0s:
String keyAllOnes = "\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF";
String keyAllZeros = "\u0000\u0000\u0000\u0000\u0000\u0000\u0000";
System.out.println("Test 1");
String plain1 = "1".repeat(64);
String enc1 = encryptBlock(plain1, KeySchedule.generateKeys(keyAllOnes));
System.out.println(enc1);
System.out.println("Test 2");
String plain2 = "0".repeat(64);
String enc2 = encryptBlock(plain2, KeySchedule.generateKeys(keyAllOnes));
System.out.println(enc2);
System.out.println("Test 3");
String plain3 = "0".repeat(64);
String enc3 = encryptBlock(plain3, KeySchedule.generateKeys(keyAllZeros));
System.out.println(enc3);
System.out.println("Test 4");
String block4 = "1100110010000000000001110101111100010001100101111010001001001100";
String enc4 = encryptBlock(block4, KeySchedule.generateKeys(keyAllZeros));
System.out.println(enc4);
System.out.println("Test 4spr");
System.out.println(block4);
System.out.println("Test 5");
String dec1 = decryptBlock("1".repeat(64), KeySchedule.generateKeysReverse(keyAllOnes));
System.out.println(dec1);
System.out.println("Test 6");
String dec2 = decryptBlock("0".repeat(64), KeySchedule.generateKeysReverse(keyAllOnes));
System.out.println(dec2);
System.out.println("Test 7");
String dec3 = decryptBlock("0".repeat(64), KeySchedule.generateKeysReverse(keyAllZeros));
System.out.println(dec3);
System.out.println("Test 8");
String block8 = "0101011010001110111001000111100001001110010001100110000011110101";
System.out.println(decryptBlock(block8, KeySchedule.generateKeysReverse(keyAllOnes)));
System.out.println("Test 9");
String block9 = "0011000101110111011100100101001001001101011010100110011111010111";
System.out.println(decryptBlock(block9, KeySchedule.generateKeysReverse(keyAllZeros)));
}
}