Generate 20 different bits
up vote
0
down vote
favorite
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.
java bit bitset des
|
show 1 more comment
up vote
0
down vote
favorite
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.
java bit bitset des
@Kartik, that was a typo :) Have edited the snippet
– Bab
Nov 19 at 0:39
I don't understand what you're trying to do here. Assuming you really want exactly 20 bits, and assuming the first 36 low-order bits are supposed to be zero, why don't you just randomly generate a 20 bit number (which can be easily done with a range parameter to theRandom()
method in Java, just pass it 2^20), and then shift left by 36 bits? That should take about 3 lines of code to implement.
– Robert Harvey♦
Nov 19 at 0:40
@RobertHarvey: After discarding the least significant bits, I want the last 36 bits to be zeros so i have a 56-bit key where the first 20-bit are randomly generated and the last 36-bits are zeros
– Bab
Nov 19 at 0:48
@Bab Edit your Question to explain that.
– Basil Bourque
Nov 19 at 0:50
And please be clear what you mean by "first" and "last." Use the terms "Most significant" and "least significant," not "first and last" (we don't know at which end of the word you are starting counting bits).
– Robert Harvey♦
Nov 19 at 0:51
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
java bit bitset des
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.
java bit bitset des
java bit bitset des
edited Nov 19 at 1:01
asked Nov 19 at 0:30
Bab
1238
1238
@Kartik, that was a typo :) Have edited the snippet
– Bab
Nov 19 at 0:39
I don't understand what you're trying to do here. Assuming you really want exactly 20 bits, and assuming the first 36 low-order bits are supposed to be zero, why don't you just randomly generate a 20 bit number (which can be easily done with a range parameter to theRandom()
method in Java, just pass it 2^20), and then shift left by 36 bits? That should take about 3 lines of code to implement.
– Robert Harvey♦
Nov 19 at 0:40
@RobertHarvey: After discarding the least significant bits, I want the last 36 bits to be zeros so i have a 56-bit key where the first 20-bit are randomly generated and the last 36-bits are zeros
– Bab
Nov 19 at 0:48
@Bab Edit your Question to explain that.
– Basil Bourque
Nov 19 at 0:50
And please be clear what you mean by "first" and "last." Use the terms "Most significant" and "least significant," not "first and last" (we don't know at which end of the word you are starting counting bits).
– Robert Harvey♦
Nov 19 at 0:51
|
show 1 more comment
@Kartik, that was a typo :) Have edited the snippet
– Bab
Nov 19 at 0:39
I don't understand what you're trying to do here. Assuming you really want exactly 20 bits, and assuming the first 36 low-order bits are supposed to be zero, why don't you just randomly generate a 20 bit number (which can be easily done with a range parameter to theRandom()
method in Java, just pass it 2^20), and then shift left by 36 bits? That should take about 3 lines of code to implement.
– Robert Harvey♦
Nov 19 at 0:40
@RobertHarvey: After discarding the least significant bits, I want the last 36 bits to be zeros so i have a 56-bit key where the first 20-bit are randomly generated and the last 36-bits are zeros
– Bab
Nov 19 at 0:48
@Bab Edit your Question to explain that.
– Basil Bourque
Nov 19 at 0:50
And please be clear what you mean by "first" and "last." Use the terms "Most significant" and "least significant," not "first and last" (we don't know at which end of the word you are starting counting bits).
– Robert Harvey♦
Nov 19 at 0:51
@Kartik, that was a typo :) Have edited the snippet
– Bab
Nov 19 at 0:39
@Kartik, that was a typo :) Have edited the snippet
– Bab
Nov 19 at 0:39
I don't understand what you're trying to do here. Assuming you really want exactly 20 bits, and assuming the first 36 low-order bits are supposed to be zero, why don't you just randomly generate a 20 bit number (which can be easily done with a range parameter to the
Random()
method in Java, just pass it 2^20), and then shift left by 36 bits? That should take about 3 lines of code to implement.– Robert Harvey♦
Nov 19 at 0:40
I don't understand what you're trying to do here. Assuming you really want exactly 20 bits, and assuming the first 36 low-order bits are supposed to be zero, why don't you just randomly generate a 20 bit number (which can be easily done with a range parameter to the
Random()
method in Java, just pass it 2^20), and then shift left by 36 bits? That should take about 3 lines of code to implement.– Robert Harvey♦
Nov 19 at 0:40
@RobertHarvey: After discarding the least significant bits, I want the last 36 bits to be zeros so i have a 56-bit key where the first 20-bit are randomly generated and the last 36-bits are zeros
– Bab
Nov 19 at 0:48
@RobertHarvey: After discarding the least significant bits, I want the last 36 bits to be zeros so i have a 56-bit key where the first 20-bit are randomly generated and the last 36-bits are zeros
– Bab
Nov 19 at 0:48
@Bab Edit your Question to explain that.
– Basil Bourque
Nov 19 at 0:50
@Bab Edit your Question to explain that.
– Basil Bourque
Nov 19 at 0:50
And please be clear what you mean by "first" and "last." Use the terms "Most significant" and "least significant," not "first and last" (we don't know at which end of the word you are starting counting bits).
– Robert Harvey♦
Nov 19 at 0:51
And please be clear what you mean by "first" and "last." Use the terms "Most significant" and "least significant," not "first and last" (we don't know at which end of the word you are starting counting bits).
– Robert Harvey♦
Nov 19 at 0:51
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can use bitmasks with the & (bitwise and) operator:
Random r = new SecureRandom(); // WARNING - use SecureRandom when generating cryptographic keys!
long v1 = r.nextLong() & 0xfffff; // Low order 20 bits are random, rest zero
long v2 = r.nextLong() & 0xfffff00000000000L; // High order 20 bits are random
In case I want to use v2 in DES, do I have to put the v2 in a BitSet of 64 bits as the high order are random?
– Bab
Nov 19 at 1:09
When using random, sometimes I get less than 64 bits where the high order is 20 bits
– Bab
Nov 19 at 13:24
Don't usenew java.util.Random
- usejava.security.SecureRandom
. And of course there could be a few leading zero bits - event if it returns zero for those 20 bits, it can still be random (it will happen about once in a million times, on average)
– Erwin Bolwidt
Nov 20 at 0:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use bitmasks with the & (bitwise and) operator:
Random r = new SecureRandom(); // WARNING - use SecureRandom when generating cryptographic keys!
long v1 = r.nextLong() & 0xfffff; // Low order 20 bits are random, rest zero
long v2 = r.nextLong() & 0xfffff00000000000L; // High order 20 bits are random
In case I want to use v2 in DES, do I have to put the v2 in a BitSet of 64 bits as the high order are random?
– Bab
Nov 19 at 1:09
When using random, sometimes I get less than 64 bits where the high order is 20 bits
– Bab
Nov 19 at 13:24
Don't usenew java.util.Random
- usejava.security.SecureRandom
. And of course there could be a few leading zero bits - event if it returns zero for those 20 bits, it can still be random (it will happen about once in a million times, on average)
– Erwin Bolwidt
Nov 20 at 0:29
add a comment |
up vote
0
down vote
You can use bitmasks with the & (bitwise and) operator:
Random r = new SecureRandom(); // WARNING - use SecureRandom when generating cryptographic keys!
long v1 = r.nextLong() & 0xfffff; // Low order 20 bits are random, rest zero
long v2 = r.nextLong() & 0xfffff00000000000L; // High order 20 bits are random
In case I want to use v2 in DES, do I have to put the v2 in a BitSet of 64 bits as the high order are random?
– Bab
Nov 19 at 1:09
When using random, sometimes I get less than 64 bits where the high order is 20 bits
– Bab
Nov 19 at 13:24
Don't usenew java.util.Random
- usejava.security.SecureRandom
. And of course there could be a few leading zero bits - event if it returns zero for those 20 bits, it can still be random (it will happen about once in a million times, on average)
– Erwin Bolwidt
Nov 20 at 0:29
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use bitmasks with the & (bitwise and) operator:
Random r = new SecureRandom(); // WARNING - use SecureRandom when generating cryptographic keys!
long v1 = r.nextLong() & 0xfffff; // Low order 20 bits are random, rest zero
long v2 = r.nextLong() & 0xfffff00000000000L; // High order 20 bits are random
You can use bitmasks with the & (bitwise and) operator:
Random r = new SecureRandom(); // WARNING - use SecureRandom when generating cryptographic keys!
long v1 = r.nextLong() & 0xfffff; // Low order 20 bits are random, rest zero
long v2 = r.nextLong() & 0xfffff00000000000L; // High order 20 bits are random
edited Nov 19 at 2:11
answered Nov 19 at 1:03
Erwin Bolwidt
23.4k123856
23.4k123856
In case I want to use v2 in DES, do I have to put the v2 in a BitSet of 64 bits as the high order are random?
– Bab
Nov 19 at 1:09
When using random, sometimes I get less than 64 bits where the high order is 20 bits
– Bab
Nov 19 at 13:24
Don't usenew java.util.Random
- usejava.security.SecureRandom
. And of course there could be a few leading zero bits - event if it returns zero for those 20 bits, it can still be random (it will happen about once in a million times, on average)
– Erwin Bolwidt
Nov 20 at 0:29
add a comment |
In case I want to use v2 in DES, do I have to put the v2 in a BitSet of 64 bits as the high order are random?
– Bab
Nov 19 at 1:09
When using random, sometimes I get less than 64 bits where the high order is 20 bits
– Bab
Nov 19 at 13:24
Don't usenew java.util.Random
- usejava.security.SecureRandom
. And of course there could be a few leading zero bits - event if it returns zero for those 20 bits, it can still be random (it will happen about once in a million times, on average)
– Erwin Bolwidt
Nov 20 at 0:29
In case I want to use v2 in DES, do I have to put the v2 in a BitSet of 64 bits as the high order are random?
– Bab
Nov 19 at 1:09
In case I want to use v2 in DES, do I have to put the v2 in a BitSet of 64 bits as the high order are random?
– Bab
Nov 19 at 1:09
When using random, sometimes I get less than 64 bits where the high order is 20 bits
– Bab
Nov 19 at 13:24
When using random, sometimes I get less than 64 bits where the high order is 20 bits
– Bab
Nov 19 at 13:24
Don't use
new java.util.Random
- use java.security.SecureRandom
. And of course there could be a few leading zero bits - event if it returns zero for those 20 bits, it can still be random (it will happen about once in a million times, on average)– Erwin Bolwidt
Nov 20 at 0:29
Don't use
new java.util.Random
- use java.security.SecureRandom
. And of course there could be a few leading zero bits - event if it returns zero for those 20 bits, it can still be random (it will happen about once in a million times, on average)– Erwin Bolwidt
Nov 20 at 0:29
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366883%2fgenerate-20-different-bits%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@Kartik, that was a typo :) Have edited the snippet
– Bab
Nov 19 at 0:39
I don't understand what you're trying to do here. Assuming you really want exactly 20 bits, and assuming the first 36 low-order bits are supposed to be zero, why don't you just randomly generate a 20 bit number (which can be easily done with a range parameter to the
Random()
method in Java, just pass it 2^20), and then shift left by 36 bits? That should take about 3 lines of code to implement.– Robert Harvey♦
Nov 19 at 0:40
@RobertHarvey: After discarding the least significant bits, I want the last 36 bits to be zeros so i have a 56-bit key where the first 20-bit are randomly generated and the last 36-bits are zeros
– Bab
Nov 19 at 0:48
@Bab Edit your Question to explain that.
– Basil Bourque
Nov 19 at 0:50
And please be clear what you mean by "first" and "last." Use the terms "Most significant" and "least significant," not "first and last" (we don't know at which end of the word you are starting counting bits).
– Robert Harvey♦
Nov 19 at 0:51