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.
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
As I understand the problem: You have an unexpected count of (set) bits!
Reason: Using Random, with the given bounds nextInt(60) and a repetition of 2 x 20 times, it is "quite likely" that you set (overwrite) your bits multiple times, which makes you missing (set) bits by the end.
A simple solution 'd be to repeat int next = nextInt(60); until !temp1.get(next) (temp2respectively):
Random r = new Random();
for (int i = 0; i < 20; i++) {
int next = r.nextInt(60);
while (temp1.get(next)) { //bit already set, repeat:
next = r.nextInt(60);
}
temp1.set(next);
// temp2:
next = r.nextInt(60);
while (temp2.get(next)) {
next = r.nextInt(60);
}
temp2.set(next);
} // ensures you 20 bits set.
A finer solution, would be a data structure, which ensures you random & unique values...like: Creating random numbers with no duplicates
or this function (see : https://stackoverflow.com/a/54608445/592355):
static IntStream uniqueInts(int min, int max, int count, java.util.Random rnd) {
// call Random.ints(min, max) with ... distinct and limit
return rnd.ints(min, max).distinct().limit(count);
}
which you would use like:
final BitSet temp1 = new BitSet(20); // values lower than 64 (resp. 32?) lead to 64 (resp. 32!)
final BitSet temp2 = new BitSet(20);
final Random r = new Random();
unniqueInts(0, 60, 20, r).forEach(i - > {if(i % 8 > 0)temp1.set(i)});
unniqueInts(0, 60, 20, r).forEach(i - > {if(i % 8 > 0)temp2.set(i)});
//also better:
key1.or(temp1);
key2.or(temp2);
System.out.println(k1temp);
//the if (i % 8 == 0) ..already done in forEach^^
Related
I'm trying to make a small program that allows you to generate a certain amount of numbers within a range, but it does not work as expected.
For example, if I ask the program to generate 3 random numbers between 5 and 10 it gives me 5 random numbers between 0 and 5.
private void jFillActionPerformed(java.awt.event.ActionEvent evt) {
int intInput1;
int intInput2;
int intInput3;
int i;
int RandomNumber;
intInput1 = Integer.parseInt(txtInput1.getText());
intInput2 = Integer.parseInt(txtInput2.getText());
intInput3 = Integer.parseInt(txtInput3.getText());
int ListSize = (intInput3) + 1;
Random rnd = new Random();
for (i = 0; i <= ListSize; i++)
{
RandomNumber = rnd.nextInt((intInput2 - intInput1) + 1);
fill.addElement(RandomNumber);
lstNumbers.setModel(fill);
}
Simply always add 5 (or more specifically - intInput1 in your case as it seems it's lower range value) to generated numbers so it will be in the range you need (0+5=5, 5+5=10 so it will be in range 5,10)
Here an IntStream you can later than use limit to set the amount of numbers you want.
public static IntStream numbersBetween(int from, int to){
return new Random().ints().map(e -> Math.abs(e % ((to+1) - from)) + from);
}
so far in the program the values were searched randomly, but I want to modify the program to search for random numbers in a given range. Generally speaking, My point is that the draw should be from the given range (from-to), and not up to 1000 random numbers as in the above code, so my question is:
How can I pass the value from and to random: rand.nextInt (?) so that the numbers are randomly drawn in a given range. so I generally need to get a printout from the program like in the question: expected output
// Create array to be searched
final int[] arrayToSearch = new int[20];
Random rnd = new Random();
for (int i = 0; i < arrayToSearch.length; i++)
arrayToSearch[i] = rnd.nextInt(1000);
System.out.println(Arrays.toString(arrayToSearch));
final int PARTITIONS = 4;
Thread[] threads = new Thread[PARTITIONS];
final int[] partitionMin = new int[PARTITIONS];
final int[] partitionMax = new int[PARTITIONS];
for (int i = 0; i < PARTITIONS; i++) {
final int partition = i;
threads[i] = new Thread(new Runnable() {
#Override
public void run() {
// Find min/max values in sub-array
int from = arrayToSearch.length * partition / PARTITIONS;
int to = arrayToSearch.length * (partition + 1) / PARTITIONS;
int min = Integer.MAX_VALUE,
max = Integer.MIN_VALUE;
for (int j = from; j < to; j++) {
min = Math.min(min, arrayToSearch[j]);
max = Math.max(max, arrayToSearch[j]);
}
partitionMin[partition] = min;
partitionMax[partition] = max;
});
so far:
partition 0: from=0, to=5, min=23, max=662 //the draw in the range 0-5, draw is outside the specified range
expected output:
partition 1: from=0, to=5, min=1, max=3 // the draw takes place within the given range 0 to 5
partition 2: from=20, to=30, min=22, max=29 //the draw takes place within the given range 20 to 30
How can I pass the value from and to random: rand.nextInt (?) so that the numbers are randomly drawn in a given range
Try it like this. This will generate values between from and to inclusive.
int from = -100;
int to = 100;
int draw = ThreadLocalRandom.current().nextInt(from, to);
You can actually generate your own Supplier to just get random numbers in a specified range.
The BiFunction returns a Supplier. And the Supplier can be called to get the a random number in the range.
BiFunction<Integer, Integer, IntSupplier> rndGen = (f,
t) -> () -> ThreadLocalRandom.current().nextInt(f, t+1);
IntSupplier rnd = rndGen.apply(from,to);
So each time rnd.getAsInt() is invoked, you will get a number in the desired range.
Note: There are of course methods that do this pretty much automatically. But I presumed you wanted to work out the logic of finding min and max yourself so I did not include those.
Class Random has method ints (long streamSize, int randomNumberOrigin, int randomNumberBound) to generate IntStream of random numbers in the given range, and then the summary statistics may be collected for such stream:
static void printMinMax(int size, int from, int to) {
IntSummaryStatistics stats = new Random()
.ints(size, from, to)
.summaryStatistics();
System.out.printf("min = %d, max = %d%n", stats.getMin(), stats.getMax());
}
Test:
printMinMax(20, 20, 200); // min = 30, max = 198
Create a random number of your choosing however you want.
If it's under your From value, add your From value to it.
If it's over your To value mod it by your To value.
I'm currently writing a random number generator that generates a pseudorandom int and then returns an int representing the least significant bit in the int value.
public class RNG {
//algorithm input: first input is the seed
private int x;
//primes for calculating next random number
private int p;
private int q;
//constructor
public RNG()
{
p = getPrime();
q = getPrime();
//make sure p and q are not equal
while(p == q)
{
q = getPrime();
}
//seed must not be divisible by p or q
//easiest way is to make the seed another prime number
//this prime does not need to be congruent to 3 mod 4, so
//getPrime() is not used.
x = (BigInteger.probablePrime(8, new Random())).intValue();
}
//return a pseudorandom prime number using java.util.Random such that
//the number is congruent to 3 mod 4
private int getPrime()
{
Random rand = new Random();
int i;
while(true)
{
//generates a random BigInteger. The probability
//this BigInteger is composite is less than 2^-100
//bitLength is 8 so maximum prime value is 251
i = (BigInteger.probablePrime(8, rand)).intValue();
if((i % 4) == (3 % 4))
{
break;
}
}
return i;
}
public int next()
{
//calculate next algorithm input
int next = (x * x) % (p * q);
//set algorithm input for next next() call
x = next;
//return least significant bit
return next & 1;
}
}
The aim of the program is to be able to create a return value that a test class can then write to a file, and because the output is the least significant bit of the randomly generated number the output will be cryptographically secure.
However, when trying to come up with the implementation of the test class, I'm having trouble trying to figure out what to do with the output. What I need the test class to be able to do is write 512 bits to an output file. The FileOuputStream class, however, is only capable of writing bytes to the output file.
What I'm trying to do is figure out if there's a way to group the individual 1 or 0 outputs from my random number generator into byte values, but so far I've not found anything that's supported in Java.
You can construct bytes (or ints, or longs) using bit shifting with the << operator (or the bit shift and assignment operator <<=)
byte b = 0;
for (int i = 0; i < 8; i++) {
b <<= 1;
b |= rng.next();
}
In a computer contest, I was given a problem where I had to manipulate input data. The input has been split() into an array where data[0] is the number of repetitions. There can be up to 10^18 repetitions. My program returned Exception in thread "main" java.lang.OutOfMemoryError: Java heap space and I failed the contest.
Here's a piece of my code that's eating up memory and CPU:
long product[][]=new long[data[0]][2];
product[0][0]=data[1];
product[0][1]=data[2];
for(int a=1;a<data[0];a++){
product[a][0]=((data[5]*product[a-1][0] + data[6]) % data[3]) + 1; // Pi = ((A*Pi-1 + B) mod M) + 1 (for all i = 2..N)
product[a][1]=((data[7]*product[a-1][1] + data[8]) % data[4]) + 1; // Wi = ((C*Wi-1 + D) mod K) + 1 (for all i = 2..N)
}
Here's some of the input data:
980046644627629799 9 123456 18 10000000 831918484 451864686 840000324 650000765
972766173386786486 123 1 10000000 10000000 590000001 680000000 610000001 970000002
299896237124947938 681206 164538 2280874 981991 416793690 904023823 813682336 774801135
My program can only work up to about 7 or 8 digits, then it takes minutes to run. With 18 digits, it crashed almost as soon as I clicked "Run" in Eclipse.
I'm curious as to how is it possible to manipulate that much data on a normal computer. Please let me know if my question is unclear or you need more information. Thanks!
You can't have, and don't need, an array of such a huge length. You just need to track the most recent 2values. E.g., just have product1 and product2.
Also, consider testing if either number is a NaN after each iteration. If so, throw an Exception and give the iteration number.
Because once you get a NaN they will all be NaN. Except you are using long, so scratch that. "Nevermind". :-)
long product[][]=new long[data[0]][2];
This is the only line in the code you pasted that allocates memory. You allocate an array whose length will be data[0] in length! As data grows, so does the array. What is the formula you're trying to apply here?
The first input data you provide :
980046644627629799
is already too large to even declare an array for. Try creating a single dimension array with that as its length and see what happens....
Are you sure you don't just want a 1 x 2 matrix that you accumulate over? Explain your intended algorithm clearly and we can help you with a more optimal solution.
Let's put the numbers into perspective.
Memory: One long takes 8 bytes. 1018 longs take 16,000,000 terabytes. Way too much.
Time: 10,000,000 operations ≈ 1 second. 1018 steps ≈ 30 centuries. Also way too much.
You can solve the memory problem by realising that you only need the most recent values at any time, and that the entire array is redundant:
long currentP = data[1];
long currentW = data[2];
for (int a = 1; a < data[0]; a++)
{
currentP = ((data[5] * currentP + data[6]) % data[3]) + 1;
currentW = ((data[7] * currentW + data[8]) % data[4]) + 1;
}
The time problem is a bit trickier to solve. Since modulus is used, you can observe that the numbers must enter a cycle at some point. Once you find the cycle, you can predict what the value will be after n iterations without having to do each iteration manually.
The simplest method for finding cycles is to keep track of whether or not you visited each element, and then go through until you encounter an element you've seen before. In this situation, the amount of memory required is proportional to M and K (data[3] and data[4]). If they are too large, a more space-efficient cycle detection algorithm must be used.
Here is an example which finds the value for P:
public static void main(String[] args)
{
// value = (A * prevValue + B) % M + 1
final long NOT_SEEN = -1; // the code used for values not visited before
long[] data = { 980046644627629799L, 9, 123456, 18, 10000000, 831918484, 451864686, 840000324, 650000765 };
long N = data[0]; // the number of iterations
long S = data[1]; // the initial value of the sequence
long M = data[3]; // the modulus divisor
long A = data[5]; // muliply by this
long B = data[6]; // add this
int max = (int) Math.max(M, S); // all the numbers (except first) must be less than or equal to M
long[] seenTime = new long[max + 1]; // whether or not a value was seen and how many iterations it took
// initialize the values of 'seenTime' to 'not seen'
for (int i = 0; i < seenTime.length; i++)
{
seenTime[i] = NOT_SEEN;
}
// find the cycle
long count = 0;
long cycleValue = S; // the current value in the series
while (seenTime[(int)cycleValue] == NOT_SEEN)
{
seenTime[(int)cycleValue] = count;
cycleValue = (A * cycleValue + B) % M + 1;
count++;
}
long cycleLength = count - seenTime[(int)cycleValue];
long cycleOffset = seenTime[(int)cycleValue];
long result;
if (N < cycleOffset)
{
// Special case: requested iteration occurs before the cycle starts
// Straightforward simulation
long value = S;
for (long i = 0; i < N; i++)
{
value = (A * value + B) % M + 1;
}
result = value;
}
else
{
// Normal case: requested iteration occurs inside the cycle
// Simulate just the relevant part of one cycle
long positionInCycle = (N - cycleOffset) % cycleLength;
long value = cycleValue;
for (long i = 0; i < positionInCycle; i++)
{
value = (A * value + B) % M + 1;
}
result = value;
}
System.out.println(result);
}
I am only giving you the solution because it looks like the contest is over. The important lesson to learn from this is that you should always check the bounds to see whether your solution is practical before you start coding it up.
I am unsure about how to generate a random n digit integer in Java using the BigInteger class.
private static Random rnd = new Random();
public static String getRandomNumber(int digCount) {
StringBuilder sb = new StringBuilder(digCount);
for(int i=0; i < digCount; i++)
sb.append((char)('0' + rnd.nextInt(10)));
return sb.toString();
}
And then you can use it:
new BigInteger(getRandomNumber(10000))
According to the docs, there is a constructor to do what you want in java 6: BigInteger(int, java.util.Random)
To that, you need only add a randomly selected 5000th digit-i.e. Use the rng constructor to 4999 digits, the add the last in via a separate random process. Actually, since you want to just sample performance for large values, you could generate the bits, and tack a one bit on the big end, rather than slave to decimal notation.
The simplest way would probably to be to fill a char[] array with 5000 random digits, convert that to a string, and then call the BigInteger(String) constructor.
If any of those steps gives you problems, please give more details.
Alternatively, you could do something like this:
Random rng = new Random(); // But use one instance throughout your app
BigInteger current = BigInteger.ZERO;
for (int i = 0; i < 5000; i++) {
BigInteger nextDigit = BigInteger.valueOf(rng.nextInt(10));
current = current.multiply(BigInteger.TEN).add(nextDigit);
}
I suspect that would be rather less efficient though.
You could reduce the number of steps required by generating nine random digits at a time, with rng.nextInt(1000000000).
Here are two versions, one takes a Random as parameter (in case you want to re-use it):
public static BigInteger getRandomNumber(final int digCount){
return getRandomNumber(digCount, new Random());
}
public static BigInteger getRandomNumber(final int digCount, Random rnd){
final char[] ch = new char[digCount];
for(int i = 0; i < digCount; i++){
ch[i] =
(char) ('0' + (i == 0 ? rnd.nextInt(9) + 1 : rnd.nextInt(10)));
}
return new BigInteger(new String(ch));
}
The resulting BigInteger will always have the specified length.
If n is between 1 to 12 then following method helps
private String getRandom(int length) {
if (length < 1 && length > 12) {
throw new IllegalArgumentException("Random number generator length should be between 1 to 12");
}
long nextLong = Math.abs(random.nextLong());
return String.valueOf(nextLong).substring(0, length);
}
One more thing to note is that it is not well tested code.
Take a string with 5000 digits in it then convert it into BigInteger.