I’m pretty tired. Don’t leak my flag while I’m asleep.
Here’s the source Sage file:
1 | from random import randint |
There’s a lot of code going on here, so rather than go step-by-step through the function calls, I’ll just summarize it for you.
super-increasing
Basically, this cryptosystem is very similar to the Merkle-Hellman cryptosystem, more commonly known as the knapsack cryptosystem. It generates what is known as a super-increasing set of numbers. Here’s an example to show you what I mean:
[1, 4, 7, 19, 32]
Basically, the next number is always greater than the sum of all previous numbers. More mathematically explained, it means:
$$\sum_{i=0}^{n-1}a_i < a_n, ; n \in [1, len(a)]$$
In the source code, this is the variable b
.
pubkey generation
Then, two numbers M
and W
are generated. W
is some sufficiently large prime that will generate the finite field over which all operations for this cryptosystem will take place. M
is basically some random number that is greater than the sum of the entire aforementioned super-increasing set.
To generate the public key, each member of the super-increasing set is multiplied by M
and then taken modulo W
. Mathematically, it is:
$$(\text{new } a_i) = (\text{old } a_i) * W ; (mod ; M)$$
Now, after this is where this cryptosystem slightly differs from the normal Merkle-Hellman cryptosystem. It randomly shuffles the public key around, making this shuffled public key the new public key, and keeps this permutation information private.
encrypting
Since the generated public key length is 8, it’s perfect for encrypting byte-by-byte. The cryptosystem iterates through all bytes of the plaintext. For each byte, it iterates through its binary representation – if it sees a 1 at index i
, it will add the number at index i
in the public key to curr
. The result for each byte is appended to a ciphertext array.
The user is then given the public key and ciphertext array.
lattices? LLL?? low density attack???
You might think that we need to pull out some lattice math to solve this knapsack cryptosystem. I thought that too during the competition, and I did actually solve it that way. In fact, if you’re interested, look up the CJLOSS low density attack
. It’s an interesting algorithm that I used to solve this problem.
but what if i don’t like lattices
That is a very based opinion (I don’t like them either). Thankfully, you don’t need to know lattices to solve this challenge!
Remember how this is a byte-by-byte encryption… and how there are only 8 bits in each byte… which means there are only $$2^8=128$$ possible plaintexts for each element of the ciphertext…
yup. just encrypt every possible plaintext byte and then map the ciphertext array back to the plaintext array.
i’m washed af
I actually didn’t even realize this until after the challenge 💀 I should just retire at this point
solve script
Anyways, here’s the solve script:
1 | import string |
uiuctf{i_g0t_sleepy_s0_I_13f7_th3_fl4g}
TL;DR:
knapsack cryptosystem with too small of a public key –> ez byte-by-byte decryption but i am blind and was probably more sleep deprived than the challenge author during the ctf