push garbage
This commit is contained in:
parent
18cdd1ed81
commit
3bc74649ad
7 changed files with 137 additions and 43 deletions
34
src/main/java/ch/fhnw/kry/CTR.java
Normal file
34
src/main/java/ch/fhnw/kry/CTR.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package ch.fhnw.kry;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class CTR {
|
||||
private final SPN spn = new SPN();
|
||||
private final int iv;
|
||||
private final int key;
|
||||
|
||||
public CTR(int key) {
|
||||
this.iv = generateIV();
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public int getIV() {
|
||||
return iv;
|
||||
}
|
||||
|
||||
public int generateIV() {
|
||||
Random random = ThreadLocalRandom.current();
|
||||
byte[] r = new byte[2];
|
||||
random.nextBytes(r);
|
||||
|
||||
return r[0] << 8 & r[1];
|
||||
}
|
||||
|
||||
public int decrypt(int block, int idx) {
|
||||
int e = (iv + idx) % (1 << 16);
|
||||
e = spn.decryptBlock(key, e);
|
||||
|
||||
return block ^ e;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue