diff --git a/src/main/java/ch/fhnw/kry/CTR.java b/src/main/java/ch/fhnw/kry/CTR.java index 7987cc9..6971620 100644 --- a/src/main/java/ch/fhnw/kry/CTR.java +++ b/src/main/java/ch/fhnw/kry/CTR.java @@ -8,8 +8,8 @@ public class CTR { private final int iv; private final int key; - public CTR(int key) { - this.iv = generateIV(); + public CTR(int iv, int key) { + this.iv = iv; this.key = key; } @@ -17,7 +17,7 @@ public class CTR { return iv; } - public int generateIV() { + public static int generateIV() { Random random = ThreadLocalRandom.current(); byte[] r = new byte[2]; random.nextBytes(r); diff --git a/src/main/java/ch/fhnw/kry/Decrypt.java b/src/main/java/ch/fhnw/kry/Decrypt.java index c4c85e8..2868fd1 100644 --- a/src/main/java/ch/fhnw/kry/Decrypt.java +++ b/src/main/java/ch/fhnw/kry/Decrypt.java @@ -7,11 +7,13 @@ import static ch.fhnw.kry.Main.BLOCK_LENGTH; public class Decrypt { public String decrypt(String keyString, String chiffre) { int key = Integer.parseInt(keyString, 2); - var ctr = new CTR(key); int[] data = strToArray(chiffre); byte[] decryptedData = new byte[data.length * 2]; - for (int i = 0; i < data.length; i++) { + int iv = data[0]; + var ctr = new CTR(iv, key); + + for (int i = 1; i < data.length; i++) { int block = data[i]; int decryptedBlock = ctr.decrypt(block, i); diff --git a/src/test/java/ch/fhnw/kry/CTRTest.java b/src/test/java/ch/fhnw/kry/CTRTest.java index 2aa54f2..01efc92 100644 --- a/src/test/java/ch/fhnw/kry/CTRTest.java +++ b/src/test/java/ch/fhnw/kry/CTRTest.java @@ -8,11 +8,9 @@ class CTRTest { @Test void generateIV() { - var ctr = new CTR(0); - int count = 0; while(count < Integer.MAX_VALUE) { - int iv = ctr.generateIV(); + int iv = CTR.generateIV(); assert (iv < 1 << 16); count++; }