I've seen it posted on different forms here and here that explain how java.util.Random
library uses a linear congruential pseudorandom number generator which can be cracked using two values. Can this type of exploit go one level deeper and guess the nextByte
data used to fill in the rest of a BigInteger value?
private static Random generator = new Random();
public static String uncrackableRandomNumber() {
return new BigInteger(128, generator);
}
A snippet of the additional layer of byte generation using java.util.Random
(rnd
).
public BigInteger(int numBits, Random rnd) {
this(1, randomBits(numBits, rnd));
}
private static byte[] randomBits(int numBits, Random rnd) {
if (numBits < 0)
throw new IllegalArgumentException("numBits must be non-negative");
int numBytes = (int) (((long) numBits + 7) / 8); // avoid overflow
byte[] randomBits = new byte[numBytes];
// Generate random bytes and mask out any excess bits
if (numBytes > 0) {
rnd.nextBytes(randomBits);
int excessBits = 8 * numBytes - numBits;
randomBits[0] &= (1 << (8 - excessBits)) - 1;
}
return randomBits;
}
The first two numbers are: 233458857748780331814340414981023411537
and 141610568161066839752374346774468879751
, how can I determine the next number given only this information?
nextBytes
does, use that to get two consecutive 32-bit outputs by the RNG, which allows to compute the full state. $\endgroup$ – fgrieu♦ May 7 '20 at 6:23nextBytes
. Glad to see the return ofnextInt
. $\endgroup$ – Todd Kennaday May 7 '20 at 6:58