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.










share|improve this question
























  • @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

















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.










share|improve this question
























  • @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















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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




















  • @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


















@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














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





share|improve this answer























  • 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 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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





share|improve this answer























  • 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 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















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





share|improve this answer























  • 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 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













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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 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


















  • 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 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]