Using Safe Random to Generate a Long Number

I sowed my safe random object with a long number. Now I want to extract another long number. But there is only a function called nextBytes(byte[] b) that gives a random byte[] .

Is there any way to get a long number?

 SecureRandom ranGen1 = new SecureRandom(); ranGen1.setSeed(1000); SecureRandom ranGen2 = new SecureRandom(); ranGen2.setSeed(1000); byte[] b1= new byte[3]; byte[] b2=new byte[3]; ranGen1.nextBytes(b1); ranGen2.nextBytes(b2); int a1=b1[0]; int a2=b1[1]; int a3=b1[2]; int c1=b2[0]; int c2=b2[1]; int c3=b2[2]; System.out.println(a1+", "+a2+", "+a3);//genearated by ranGen1 System.out.println(c1+", "+c2+", "+c3);//generated by ranGen2 System.out.println(ranGen1.nextLong());//genearated by ranGen1 

System.out.println (ranGen2.nextLong ()); // generated by ranGen2

result:

 4, -67, 69 4, -67, 69 -3292989024239613972 //this is using nextLong() -3292989024239613972 

Result for Peter Lowry code: (Using safe random)

 -7580880967916090810 -7580880967916090810 7364820596437092015 7364820596437092015 6152225453014145174 6152225453014145174 6933818190189005053 6933818190189005053 -2602185131584800869 -2602185131584800869 -4964993377763884762 -4964993377763884762 -3544990590938409243 -3544990590938409243 8725474288412822874 8725474288412822874 -8206089057857703584 -8206089057857703584 -7903450126640733697 -7903450126640733697 

They are superior to the same. How can you get different numbers?

This is the result that I get after using the second update of Peter Lawrey (I am using the Windows operating system and it seems to be using a different operating system that created the confusion)

 SHA1PRNG appears to produce the same values with the same seed The default PRNG on this system is SHA1PRNG 
+4
source share
4 answers

Revised again, this is the correct answer! (and I should follow my own advice and read the documentation carefully)

Is this what you use? If so, it extends Random, so it has an inherited nextLong () method. Since it overrides next (), all typical random methods will use the SecureRandom PRNG method.

(see comments why my second answer is incorrect or rather not needed)

I would suggest creating a long one by simply composing it from the next 8 bytes or two ints (returned by the next). There is no problem with this, and I see no reason why you could not touch all the long values ​​(think that any of the two 32-bit halves can have values ​​from 0 to 2 ^ 32, with equal probability) or why one was would be more likely than another (which would mean it is not pseudo-random).

I don’t quite understand why the Random documentation indicates this restriction for nextLong (), but I believe that this is a limitation of the linear algorithm used (I think that linear algorithms have a much shorter cycle, i.e. when they start repeating numbers - than modern PRNG). I think it's worth paying attention to exchanging a cryptographic stack for curiosity.

+5
source

SecureRandom extends Random and Random has the nextLong() method: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextLong%28%29

+2
source

Note. When using Random, the given seed will always give the same results. With SecureRandom this will not happen. The seed just adds randomness.

Have you ever used security? The whole point of the seed is to produce the same number of numbers. This also applies to safe random. Two safe random numbers seeded with the same value produce the same sequence of random numbers.

 public static void main(String... args) throws NoSuchProviderException, NoSuchAlgorithmException { testRNG("NativePRNG"); testRNG("SHA1PRNG"); System.out.println("The default PRNG on this system is " + new SecureRandom().getAlgorithm()); } private static void testRNG(String prng) throws NoSuchAlgorithmException, NoSuchProviderException { SecureRandom sr1 = SecureRandom.getInstance(prng, "SUN"); SecureRandom sr2 = SecureRandom.getInstance(prng, "SUN"); sr1.setSeed(1); sr2.setSeed(1); for (int i = 0; i < 10; i++) { if (sr1.nextLong() != sr2.nextLong()) { System.out.println(prng + " does not produce the same values with the same seed"); return; } } System.out.println(prng + " appears to produce the same values with the same seed"); } 

prints

 NativePRNG does not produce the same values with the same seed SHA1PRNG appears to produce the same values with the same seed The default PRNG on this system is NativePRNG 

go and try again

Good advice, but just trying does not always give you the whole answer in this case.

+2
source
 BigInteger randomNumber = new BigInteger(numBits, random); 
+1
source

Source: https://habr.com/ru/post/1400711/


All Articles