-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeistelFunctions.java
More file actions
32 lines (22 loc) · 952 Bytes
/
FeistelFunctions.java
File metadata and controls
32 lines (22 loc) · 952 Bytes
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
import java.util.*;
public class FeistelFunctions {
public static String xorIt(String bin1, String bin2) {
if (bin1.length() != bin2.length()) {
throw new IllegalArgumentException("xorIt requires same-length binary strings: " + bin1.length() + " vs " + bin2.length());
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < bin1.length(); i++) {
result.append(bin1.charAt(i) == bin2.charAt(i) ? '0' : '1');
}
return result.toString();
}
public static String functionF(String rightHalf, String subkey) {
String xorResult = xorIt(rightHalf, subkey);
// apply SBox substitution
StringBuilder sBoxResult = new StringBuilder();
for (int i = 0; i < 32; i += 8) {
sBoxResult.append(SBox.substitution(xorResult.substring(i, i + 8)));
}
return Permutation.permute(sBoxResult.toString());
}
}