diff --git a/bin/main/ch/fhnw/kry/SPN.class b/bin/main/ch/fhnw/kry/SPN.class new file mode 100644 index 0000000..e5a110b Binary files /dev/null and b/bin/main/ch/fhnw/kry/SPN.class differ diff --git a/bin/test/ch/fhnw/kry/SPNTest.class b/bin/test/ch/fhnw/kry/SPNTest.class new file mode 100644 index 0000000..b5648b6 Binary files /dev/null and b/bin/test/ch/fhnw/kry/SPNTest.class differ diff --git a/src/main/java/ch/fhnw/kry/SPN.java b/src/main/java/ch/fhnw/kry/SPN.java index 92de8d7..d0f5e49 100644 --- a/src/main/java/ch/fhnw/kry/SPN.java +++ b/src/main/java/ch/fhnw/kry/SPN.java @@ -1,7 +1,40 @@ 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 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; @@ -9,4 +42,23 @@ public class SPN { 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; + } + } diff --git a/src/test/java/ch/fhnw/kry/SPNTest.java b/src/test/java/ch/fhnw/kry/SPNTest.java index 0f0d838..3a29036 100644 --- a/src/test/java/ch/fhnw/kry/SPNTest.java +++ b/src/test/java/ch/fhnw/kry/SPNTest.java @@ -15,4 +15,22 @@ class SPNTest { assertEquals(0xFFFF, spn.k(key, 1)); assertEquals(0xFFFF, spn.k(key, 2)); } + + @Test + void substitution() { + var spn = new SPN(); + + //given + int x = 0xEF45; + int r = 0x051C; + System.out.print(Integer.toHexString(x)); + + //when + int y = spn.substitution(x); + + // then + assertEquals(r, y); + + // System.out.print(Integer.toHexString(spn.substitution(x))); + } }