ammcom
ammcom

Reputation: 1034

Generate same Random BigInteger in Java for android

I am trying to generate a random BigInteger in Java, but I need to generate the same value when using the same seed for the secure random:

BigInteger P = new BigInteger(1024,1,new SecureRandom(SEED));

Strangely the above line of code generate a different value even if the SEED value is the same, what is going wrong?

Upvotes: 1

Views: 382

Answers (2)

ammcom
ammcom

Reputation: 1034

Actually I figured it out, I am developing an Android Application and the implementation of BigInteger class in Android seems to be different, in Android the documentation says that the constructor will ignore the specified Random object if the bit length is greater than 16 and will use OpenSSL BN_generate_prime_ex as a source for random numbers Edit: Because someone may get use of this, I have implemented a new big integer class that has the same code as the java SE version, and figure out that it is a performance issue that made the android Sdk creators to change the implementation of this very class as my class ran very slow that it is not applicable in a real application, we should find some C++ implementation and use the NDK

Upvotes: 1

Kayaman
Kayaman

Reputation: 73548

If you want your randomness to be deterministic, don't use SecureRandom. Its whole point is to provide a better and more secure source of randomness than the regular Random which provides you with pseudo-random values with a deterministic algorithm, allowing (and forcing) you to always get the same values with the same seed.

The difference with SecureRandom is that for one it's not dependent on a single algorithm, but uses the SPI mechanism to allow implementations to generate randomness from all kinds of different sources.

Upvotes: 4

Related Questions