65 lines
1.5 KiB
Java
65 lines
1.5 KiB
Java
package ch.fhnw.kry;
|
|
|
|
import java.util.Map;
|
|
import java.util.HashMap;
|
|
import static java.util.Map.entry;
|
|
|
|
public class SPN {
|
|
private final static int ROUND_KEY_LENGTH = 16;
|
|
private final Map<Integer, Integer> SBOX = Map.ofEntries(
|
|
entry(0xE, 0),
|
|
entry(4, 1),
|
|
entry(0xD, 2),
|
|
entry(1, 3),
|
|
entry(2, 4),
|
|
entry(0xF, 5),
|
|
entry(0xB, 6),
|
|
entry(8, 7),
|
|
entry(3, 8),
|
|
entry(0xA, 9),
|
|
entry(6, 0xA),
|
|
entry(0xC, 0xB),
|
|
entry(5, 0xC),
|
|
entry(9, 0xD),
|
|
entry(0, 0xE),
|
|
entry(7, 0xF)
|
|
);
|
|
|
|
public String decrypt(int key, String chiffre) {
|
|
|
|
return null;
|
|
}
|
|
|
|
public int sp (int key, int x) {
|
|
x = init(key, x);
|
|
|
|
return x;
|
|
}
|
|
|
|
public int k(int key, int i) {
|
|
i *= 4;
|
|
int mask = 0xFFFF_0000 >>> i;
|
|
|
|
return key & mask >>> ROUND_KEY_LENGTH - i;
|
|
}
|
|
|
|
public int init(int key, int x) {
|
|
return x ^ k(key, 4);
|
|
}
|
|
|
|
public int substitution(int x) {
|
|
int mask = 0xF000;
|
|
for (int i = 0; i < 4; i++) {
|
|
int j = 4 * i;
|
|
int key = (x & mask) >>> 12 - j;
|
|
int val = SBOX.get(key);
|
|
int replMask = ~(0xf << 12 - j); // Maske mit 4 0-Bits vorbereiten zur Vorbereitung vom Einfügen
|
|
x = x & replMask | (val << 12 - j); // x an der Stelle der 4 0-Bits mit val überschreiben
|
|
|
|
mask = mask >>> 4; // maske um 4 Bit für nächste Iteration verschieben
|
|
}
|
|
return x;
|
|
}
|
|
|
|
}
|