PRNG-based stream cipher
The idea of a stream cipher is simple: the generator produces a long keystream, and the message is XORed with it. If the generator is truly cryptographic, this is secure. If it is a simple LCG — the entire cipher becomes as weak as the predictability of this generator.
Here every 4 bytes of the keystream is the complete internal state of the LCG (modulus 232). That is, if we know a part of the plaintext, we can directly read the keystream from that place, and the internal states from the keystream.
Leaked encryption code from the company's internal messenger (A and C remained secret):
M = 2**32
A = <maxfiy>
C = <maxfiy>
def kalit_oqimi(urug, n):
x, out = urug, b''
while len(out) < n:
x = (A * x + C) % M
out += x.to_bytes(4, 'big') # har qadamda 4 bayt
return out[:n]
ct = bytes(p ^ k for p, k in zip(xabar, kalit_oqimi(urug, len(xabar))))
Important information: every message in this messenger starts exactly with the following 16 characters:
SALOM CYBERSAMSU
The attached xabar.hex file contains the ciphertext of one captured message. Fully decrypt it.
Roadmap
- XOR the known header with the first 16 bytes of the ciphertext — this yields 16 bytes of the keystream.
- Split these 16 bytes into 4-byte chunks and convert them into big-endian numbers: these are consecutive internal states x1, x2, x3, x4.
- Find A using the differences:
A = (x3 - x2) * (x2 - x1)^(-1) mod 2^32, thenC = (x2 - A*x1) mod 2^32. Check with x4. - Starting from x4, step forward to generate the entire keystream and fully decrypt the ciphertext.
Note: the inverse element modulo 2^32 exists only for odd numbers. In this challenge x2 - x1 is odd — so pow(x2 - x1, -1, 2**32) works fine.