I am working on encryption with DES, which uses a 56-bit effective key (after discarding the least significant bits) to encrypt a 64-bit plaintext. I want to set the first 20-bits of the key to random bits, and the last 36-bits to 0. I have been trying to do it with BitSet
where I have set up an array with with 64-bits where all the values are false in the beginning. Then I have set up a temp array of 20-bits, and I have been trying to use bitset.set(Random.nextInt(n), true)
within a for loop, which goes from 0-20 - The idea is that I get exactly 20 random bits.
In order to discard the least significant bit, I have a for loop where I go from 0 to 20. Within this for loop, I have an if statement which discards every 8th element for the first 20 elements
static BitSet key1 = new BitSet(64);
static BitSet key2 = new BitSet(64);
public static void generate() {
BitSet temp1 = new BitSet(20);
BitSet temp2 = new BitSet(20);
Random r = new Random();
for (int i = 0; i < 20; i++) {
temp1.set(r.nextInt(60), true);
temp2.set(r.nextInt(60), true);
}
for (int i = 0; i < 20; i++) {
key1.set(i, temp1.get(i));
key2.set(i, temp2.get(i));
}
System.out.println(k1temp);
for (int i = 0; i < temp1.length(); i++) {
if (i % 8 == 0) {
key1.clear(i);
key2.clear(i);
}
}
}
So, the problem I have is that my BitSet
does not always consist of 20 elements, which leads to that the key I generate is wrong. I have been through the code several times, but I cannot see what is wrong.
EDIT:
What I mean by first and last is that the first bits are the Most significant bits and the last are the Least significant bits.