Linear Congruential Generator
A Linear Congruential Generator (LCG) is one of the oldest and simplest pseudo-random number generators. It is fast but not cryptographically secure, so finding one in a challenge is usually a sign of a weakness.
Definition
An LCG is defined by a modulus , a multiplier , an increment and a seed . It produces its sequence with the recurrence:
The closed form after steps is:
The generator reaches its maximal period (length ) only when , and satisfy the Hull-Dobell theorem conditions. Otherwise the period can be much shorter and the output can degenerate.
Attacks
Known or guessable parameters
The whole sequence is predictable once , and are known. A frequent mistake is deriving them from a low-entropy source, for example the current time (
a = int(time.time())), which can be brute-forced over a small window. When the parameters are unknown but several consecutive outputs are available, they can also be recovered by solving the recurrence.Degenerate state collapsing to a constant
If the radical of divides (i.e. is a multiple of the product of the distinct prime factors of ), then for large enough. The first term of the closed form vanishes and the state becomes a constant fixed point:
A stream cipher built on such a generator then reuses the same keystream block, and is broken with a single known-plaintext (for example the flag format).