From 0851f73c88f65bf570376f0b0cd7bdfbbc4103ea Mon Sep 17 00:00:00 2001 From: Simon Freiermuth Date: Thu, 17 Mar 2022 14:40:35 +0100 Subject: [PATCH] Substituiton --- bin/main/ch/fhnw/kry/SPN.class | Bin 0 -> 2005 bytes bin/test/ch/fhnw/kry/SPNTest.class | Bin 0 -> 1061 bytes src/main/java/ch/fhnw/kry/SPN.java | 52 +++++++++++++++++++++++++ src/test/java/ch/fhnw/kry/SPNTest.java | 18 +++++++++ 4 files changed, 70 insertions(+) create mode 100644 bin/main/ch/fhnw/kry/SPN.class create mode 100644 bin/test/ch/fhnw/kry/SPNTest.class diff --git a/bin/main/ch/fhnw/kry/SPN.class b/bin/main/ch/fhnw/kry/SPN.class new file mode 100644 index 0000000000000000000000000000000000000000..e5a110b781695a9a792f4e01d4ad11b402c0f575 GIT binary patch literal 2005 zcma)7T~ixn6n@@pl1;)wmeA1JgxV^WkZM!E0&NJ@QcBYXa0oIoUUZhQkcD)^)Kx4mT5JGMq(Qy^HmHUANz%L=B`cIvrVYpWm-tvKth)$TNHfw7a( z3C~nDOK#gzW0eklXz@RET3NKP@oCNP*OjlQ6K4^kM?k>=nhDj2wcmjyz$EF@6Kbme*s zb`Rrw`~rvkcwH7`iAx4v!K>t}u_jx0?3Tbt=3@`^)csQ^g(w7GGY~~gZoqNvxz58C zySZqs)X93H+^AahC9CO3+jj=rHHQ`Y%crzJuJu*B+T7lv07f#UuAwUI%3UvXbL6|Y zmQ9-+=+&CDwk9{7_-~#-ptVK5GNsaJDaMZ9GVnI8ahYB2y%Cl&5ii9`2CFMbfwm6CL#93zGdM2Jn5i$C~5c+O8k1X0mPtPvs_TmWfDhG2OI+fo?OY%0BnH;d`7X?UP9{sq4cI)=M|qkcRT3Ce zRieB*&XdMUe2@KlGt5(iq_Zh~57(a@m+{!i{qG?PnD9ZL6EwmhmJ-FHHu)6y^r2cl zm`d-OMl!g%lMiLT!*Ein`Ugw~@C%ZAxRno^aTR|Yc#_vsNqsjN_Uun5()v#EGffQX zUmZM4$qqHOzbusl4x^FC2ubWko|+irw#IP-*KrpGlo?y(zf!s51+S|kl9tp0q%JCo zngCz&c}15-teaF~n3J62EvbbNaW7zor!dhIL=SVxCE{}BefY7 zA4M;KXyOC-GQNTM?Y7#8TgBe%Ql zy7YGKdP@qVDsIDW)of3N>;9$|>?vO$UAe%zC?F05re1ZOK(%E}`HG2~j^hS)pj?M} znTqQ*t^F=hICfMZJ(=A zw?=Rqi#itmx$o+cAIP?e9P-R|yS(~zC3+6!T05R{f@3XpLK|*#u$aC#yN*VAE8Ck%ioZEeS(QftdzP^h{cbQKE-4h zYJ)J*3kQDF)B@dx>0!_zP5msj3oJNf$o91R48xem4b0Nr;9X>KlRRK_5!WaFOYraF zAFk9~DVfM@e8GI?6idgrTgV(^ZB3g=YXlu2-H6Ic6KRqNQv{mkI77QE7BClKt@fn< fV$JrkM%lwpBCIiHKV=kWmX0DLjo9l1ilOuyG=s~V literal 0 HcmV?d00001 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))); + } }