defund made a simple OFB cipher, if you can even call it that. Here’s the source and the encrypted flag.

Let’s look at the encryption script first.

First, it reads the flag.png and if the total number of bytes are not divisible by 4, it appends null bytes at the end of it to make it divisible by 4.

Next, it divides the byte array to the 4-byte blocks. Then, it iterates over each block, converts them to unsigned integers, xors them with random numbers that are generated by Linear Congruential Generator (LCG), and packs the result as a big-endian .

In order to crack the LCG algorithm, we need to find 2 equations since we have 2 unknowns. We know the original file is png and every png starts with the following 16 bytes:

So, we already know the first 4 blocks of the original file. If we convert these 4 blocks to 4 integers and xor them with the encrypted ones, we will retrieve the first 4 random number that are generated by the LCG. Actually, first value of x is the seed of the LCG and the rest are generated by the LCG.

Now, we know x0, x1, x2, x3 values. We also know that x[n + 1] = (a * x[n] + c) % m.

Let’s write down two equations.

x[1] = a * x[0] + c (mod m)
x[2] = a * x[1] + c (mod m)

We can solve these equations for a.

a = (x[2] – x[1]) / (x[1] – x[0]) (mod m)

Note that we need to use modular inverse since the formula includes modular division.

After we find a, we can calculate c by using one of the equations above.

Finally, we will generate the same numbers the encryption function did and we will xor the encrypted file’s blocks with the same values. It is because k^k=0 and p^0=p. It means that if you xor a number with the same value twice, it will stay the same, i.e. k^p^p=k^0=k.

Here is the decryption script.

After running the script, we got the following image: