404ctf/solve.py

38 lines
1.2 KiB
Python

class Verify:
def __init__(self, bits):
self.k = bits
self.n = len(bits)
def verify_all(self):
assert all(sum(self.k[i:i + 4]) % 2 == 1 for i in range(0, self.n, 4)), "E1"
assert all(self.k[2 * i + 2] for i in range(self.n // 4) if not (self.k[i] == self.k[4 * i])), "E2"
assert all(len(set(self.k[i:i + 3])) > 1 for i in range(self.n - 2)), "E3"
assert all(sum(self.k[i:i + 8]) == 4 for i in range(self.n - 7)), "E4"
assert all(self.k[i] ^ self.k[self.n - 1 - i] for i in range(self.n // 2)), "E5"
for n in range(256):
bits = [ int(b) for b in format(n, f'08b')*(128//8) ]
v = Verify(bits)
try:
v.verify_all()
print("ok for", n)
enc = bytes.fromhex("868286f1e6f4c9e182dff7c683ffd796ed80eddfe7f186ed83c1ed8582fdedffc7f1dacf")
key_bytes = int(''.join(map(str, bits)), 2).to_bytes(128 // 8, 'big')
flag = ""
for i, c in enumerate(enc):
flag += chr(c^key_bytes[i % len(key_bytes)])
print(flag)
break
except AssertionError:
pass
except Exception as e:
print(e)
pass