Cryptographically secure PRNG (PseudoRandom Number Generator)

1.2k views Asked by At

What is the best and fastest algorithm to generate cryptographically secure PRNG? My requirements is as follows.

  • In each run, I need to generate few millions of numbers with length of 10.
  • It should not allow one to predict future iterations by observing some number of iterations.
  • Numbers should be unique. No duplicates allowed.
  • Speed is also important since it will generate millions of numbers in each iteration.

I checked Mersenne Twister algorithm but it seems it is not cryptographically secure. They said, it could be predicted by checking 624 iterations.

P.S. It would be better if there is a Java Implementation.

2

There are 2 answers

1
rossum On

Your unique requirement means that you cannot use any sort of RNG (my earlier comment was wrong), since random numbers will include repeats. Instead, use a cipher and encrypt the numbers: 0, 1, 2, 3, ... which will give guaranteed unique results. Since ciphers are reversible, each cyphertext decrypts back to the original plaintext, so the cyphertexts are a permutation of the plaintexts.

You also want numbers of "length ten". I assume this means ten decimal digits, [1,000,000,000 .. 9,999,999,999]. That means you need a cipher which works in the range [0 .. 8,999,999,999], and just add 1e9 to the output.

That is more complex. Either use the Hasty Pudding cipher, which can be set for any range of numbers you want, or roll your own Feistel cipher with its block size set to the next higher power of 2. If a number is out of range then re-encrypt it until it is in range. The Feistel option will be faster, but less secure. You can make it more secure by increasing the number of rounds at the cost of making it slower.

0
AudioBubble On

Use Linear feedback shift register. This algorithm is fast and secure. Build register based on primitive characteristic polynomial of degree n. This will give the sequence of 2^n - 1 unique numbers. After that the sequence will repeat. Do not seed with zero. Some well-known stream cipher algorithms are based on LFSR, so you can extract and investigate implementation. For example LILI-128 (but its reference implementation in C)