Substituiton
This commit is contained in:
parent
682cd6f814
commit
0851f73c88
BIN
bin/main/ch/fhnw/kry/SPN.class
Normal file
BIN
bin/main/ch/fhnw/kry/SPN.class
Normal file
Binary file not shown.
BIN
bin/test/ch/fhnw/kry/SPNTest.class
Normal file
BIN
bin/test/ch/fhnw/kry/SPNTest.class
Normal file
Binary file not shown.
@ -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<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;
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user