Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 40 ยท Math Foundations

Randomness, Entropy and Secure Randomness

Learn the difference between a PRNG and a CSPRNG, how entropy is measured, why Python's random module is unsafe for keys, and what nonce reuse breaks.

Every secret in cryptography starts as a random number: keys, salts, nonces, session tokens, the primes inside an RSA key. If an attacker can guess or reproduce your randomness, the strength of the algorithm that uses it does not matter. Many real-world breaks were not attacks on AES or RSA but on a weak random number generator feeding them.

This lesson separates three ideas that are often mixed up: ordinary pseudorandom generators that are good for simulations, cryptographically secure generators that are good for secrets, and entropy, the measure of how unpredictable something really is. You will also see what goes wrong when a nonce is used twice, a theme that returns in stream ciphers and authenticated encryption.

PRNG versus CSPRNG

A pseudorandom number generator (PRNG) is a deterministic algorithm. It starts from a small value called the seed, and then produces a long sequence that looks random. Give it the same seed and you get exactly the same sequence. That is a feature for simulations, tests and games, and it is the reason every example on this site uses a fixed seed so that output is reproducible.

A cryptographically secure PRNG (CSPRNG) adds a stronger requirement: even if the attacker has seen all earlier outputs, they cannot predict the next bit better than a coin flip, and cannot work out the internal state. It must also be seeded with real, unpredictable entropy from the operating system (timing jitter, hardware noise, device events).

flowchart LR
    E["Entropy sources<br/>hardware, timing, events"] --> S["Seed / internal state"]
    S --> G["Generator algorithm"]
    G --> O["Output stream"]
    G -->|"state update"| S

Both kinds of generators have this shape. What differs is how hard it is to recover the state from the output, and how unpredictable the seed was.

In Python, the random module is a fast, non-secure generator (the Mersenne Twister). The secrets module draws from the operating system's CSPRNG and is the right choice for anything an attacker would like to guess.

Need Use
Shuffle a deck in a game, sample test data random
Key, salt, nonce, session token, password reset link secrets or os.urandom

Why a predictable generator is dangerous

The simplest generator is the linear congruential generator (LCG): x_next = (a * x + c) mod m. It is easy to write and passes casual inspection, but its entire future is determined by a single output. The Mersenne Twister is much better statistically, yet it still leaks: after seeing 624 consecutive 32-bit outputs, an attacker can reconstruct its full state. Neither is acceptable for secrets.

A second classic mistake is seeding with something guessable, such as the current time in seconds. The generator may be strong, but the seed has only a few thousand plausible values, so the attacker simply tries them all.

Python Predicting an LCG and guessing a time seed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Fewer than four thousand tries is nothing. The same idea applies whenever a seed has fewer than about 264 possibilities that the attacker can enumerate.

Reproducible on this site, random in real life

Because the examples must print the same text every time, many lessons use random.Random(seed). That is a teaching device only. In production code you never choose the seed for secrets, and you never use random for them. The operating system provides unpredictable bytes through secrets (or os.urandom), and the rest of the generator machinery is hidden inside the OS.

Warning: Do not seed a generator with a constant or a timestamp when generating keys, nonces or tokens. And do not "improve" a generator by mixing outputs yourself. Ask the OS for bytes.

Measuring entropy

Entropy measures unpredictability in bits. If a secret is chosen uniformly from N equally likely possibilities, it has log2(N) bits of entropy, which is also the number of yes/no questions an attacker needs to ask in the worst case. A random 8-letter lowercase password has 8 * log2(26), about 37.6 bits. A random 128-bit key has 128 bits, which means 2128 possibilities.

Two cautions. First, entropy describes the process that chose the secret, not the string you see. correct horse battery staple has high entropy only if the words were drawn at random from a list; if a person invented it, it has far less. Second, entropy is not the same as the statistical "randomness" of the digits. The digits of pi look random but have almost zero entropy as a secret, because everyone knows where they came from.

Python Bits of entropy for different secrets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The Shannon estimate from a sample says what the symbol frequencies look like, not how the secret was chosen: abcdefgh scores 3 bits per symbol, yet if you typed it from the alphabet in order, its real entropy as a password is nearly zero. Always reason about the generating process.

The secrets toolbox

  • secrets.token_bytes(n) returns n random bytes, the right source for a symmetric key or a nonce.
  • secrets.token_hex(n) and secrets.token_urlsafe(n) return text tokens for session identifiers or reset links.
  • secrets.randbelow(n) returns an integer in [0, n) without modulo bias.
  • secrets.choice(sequence) picks one element.
  • secrets.compare_digest(a, b) compares two secrets in constant time (see MACs and HMAC).

Nonce reuse

A nonce ("number used once") is a value that must never repeat for a given key. Many ciphers turn the key plus the nonce into a keystream that is combined with the plaintext by XOR. If the same key and nonce are used for two messages, the same keystream is applied twice. XOR-ing the two ciphertexts then cancels the keystream completely and leaves the XOR of the two plaintexts, which is often enough to recover both.

Python What happens when a nonce repeats

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The attacker never learned the key. They only needed to guess that one message begins with ATTACK. This flaw destroyed real systems, from wartime ciphers to the early WEP Wi-Fi protocol. The one-time pad lesson, XOR and the one-time pad, takes this further.

How do you avoid reuse? Either use a counter that you never reset, or choose nonces randomly from a space big enough that a collision is negligible (96-bit random nonces are only safe for about 232 messages per key, a limit that comes from the birthday bound in hash attacks). Some modern schemes are designed to be nonce-misuse resistant, but you should not rely on that.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is Python's random module unsuitable for generating encryption keys?

    random is a deterministic statistical generator. Observing enough output or guessing the seed lets an attacker reproduce everything it will produce. Use secrets or os.urandom.
  2. 2. How many bits of entropy does a uniformly random 6-digit PIN have?

    There are 10^6 equally likely PINs, and log2(1,000,000) is about 19.9 bits.
  3. 3. A token is generated with random.Random(int(time.time())). What is the main weakness?

    If the attacker can estimate when the token was made, only a few thousand seeds are possible, and each can be tested quickly.
  4. 4. What does XOR-ing two ciphertexts encrypted with the same key and nonce reveal?

    The identical keystreams cancel out, leaving plaintext1 XOR plaintext2, which an attacker can often split apart using guessed text.

Summary

  • A PRNG is a deterministic generator from a seed; a CSPRNG resists prediction and is seeded from operating-system entropy.
  • Use secrets or os.urandom for keys, salts, nonces and tokens; use random only for simulations. Fixed seeds on this site exist purely for reproducible lessons.
  • Entropy is log2 of the number of equally likely possibilities, and describes how the secret was chosen, not how it looks.
  • Predictable seeds (a timestamp, a constant) and linear generators let attackers reproduce your "random" values.
  • Reusing a nonce with the same key makes a stream cipher leak the XOR of the plaintexts.

Next: Cryptographic Hash Functions.