LCG: recovering the seed
LCG (Linear Congruential Generator) is the oldest and simplest PRNG: x[i+1] = (a * x[i] + c) mod m. Three parameters — the multiplier a, the increment c, and the modulus m.
The main vulnerability of an LCG is that it works in both directions. If we solve the formula for x[i]: x[i] = a^(-1) * (x[i+1] - c) mod m. Here a^(-1) is the modular inverse of a modulo m. That is, if we know one internal state, we can step the generator backward as much as we want and find the starting seed.
The program encrypted the flag like this:
m = 2**32
a = 1664525
c = 1013904223
def keyingi(x):
return (a * x + c) % m
x = urug # x_0 — maxfiy urug'
kalit = []
for i in range(44):
x = keyingi(x) # x_1, x_2, x_3, ...
kalit.append((x >> 24) & 0xFF) # holatning eng yuqori bayti
ct = bytes(b ^ k for b, k in zip(bayroq, kalit))
# ... dastur yana ishlashda davom etdi ...
The debugger showed the internal state when the program had completed the 50th step:
x_50 = 3116764485
Encrypted flag (hex):
2010916f31615abce1b6b5ab5ab2a1955f4e53e8de70a38adb93de20b6d5e785
bc3f9729698d79589fcaf9cc
Modular inverse in Python: pow(a, -1, m) (Python 3.8+). Since a is odd, the modular inverse modulo 2^32 certainly exists.