Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 440
verybadalloc Avatar asked Mar 14 '14 03:03

verybadalloc


People also ask

Is PyCrypto deprecated?

PyCrypto 2. x is unmaintained, obsolete, and contains security vulnerabilities.

What are random bytes?

Random#bytes() is a Random class method which returns random binary string containing size bytes. Syntax: Random.bytes()

How do you generate random bytes in Python?

urandom() method is used to generate a string of size random bytes suitable for cryptographic use or we can say this method generates a string containing random characters. Return Value: This method returns a string which represents random bytes suitable for cryptographic use.


1 Answers

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.

like image 177
user515430 Avatar answered Oct 25 '22 00:10

user515430