What is the difference between Pycrypto's Random.get_random_bytes and a simple random byte generator?

StackOverflow https://stackoverflow.com/questions/22395478

  •  14-06-2023
  •  | 
  •  

Question

I came across this method in Pycrypto, which is used to generate random bytes:

from Crypto import Random
Random.get_random_bytes(5)

I was wondering how this method is different from a simple generator like the following:

import random
def get_random_bytes(N):
    ASCII = "".join(chr(x) for x in range(255))
    return "".join(random.choice(ASCII) for _ in range(n))

Note: my intuition is that the Pycrypto method is more cryptographically "sound". Looking at random's documentation, it says that it is based on a generator with a period of 2**19937-1. Looking at Random.get_random_bytes, it states that it is capable of generating cryptographically-strong bytes. What does that mean?

Of course, I wish to use the library implementation, instead of my own. I just want to understand the cryptography concepts behind it.

Was it helpful?

Solution

For a cryptographically secure random number generator any sequence of output provides you no information as for what the next output will be.

random is based on the Mersenne Twister. It has an internal state of 624 32-bit numbers. Given the output of 1248 values you know the entire state at some point. From that you can with 100% accuracy determine what all future outputs will be.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top