read iv from chiffre (still not correct but at least less wrong now)

This commit is contained in:
Sebastian Hugentobler 2022-03-22 19:38:52 +01:00
parent 3bc74649ad
commit e04a341fc6
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0
3 changed files with 8 additions and 8 deletions

View File

@ -8,8 +8,8 @@ public class CTR {
private final int iv; private final int iv;
private final int key; private final int key;
public CTR(int key) { public CTR(int iv, int key) {
this.iv = generateIV(); this.iv = iv;
this.key = key; this.key = key;
} }
@ -17,7 +17,7 @@ public class CTR {
return iv; return iv;
} }
public int generateIV() { public static int generateIV() {
Random random = ThreadLocalRandom.current(); Random random = ThreadLocalRandom.current();
byte[] r = new byte[2]; byte[] r = new byte[2];
random.nextBytes(r); random.nextBytes(r);

View File

@ -7,11 +7,13 @@ import static ch.fhnw.kry.Main.BLOCK_LENGTH;
public class Decrypt { public class Decrypt {
public String decrypt(String keyString, String chiffre) { public String decrypt(String keyString, String chiffre) {
int key = Integer.parseInt(keyString, 2); int key = Integer.parseInt(keyString, 2);
var ctr = new CTR(key);
int[] data = strToArray(chiffre); int[] data = strToArray(chiffre);
byte[] decryptedData = new byte[data.length * 2]; 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 block = data[i];
int decryptedBlock = ctr.decrypt(block, i); int decryptedBlock = ctr.decrypt(block, i);

View File

@ -8,11 +8,9 @@ class CTRTest {
@Test @Test
void generateIV() { void generateIV() {
var ctr = new CTR(0);
int count = 0; int count = 0;
while(count < Integer.MAX_VALUE) { while(count < Integer.MAX_VALUE) {
int iv = ctr.generateIV(); int iv = CTR.generateIV();
assert (iv < 1 << 16); assert (iv < 1 << 16);
count++; count++;
} }