from pwn import *
import re

def ROL8(x, n):
    n = n % 8
    return ((x << n) | (x >> (8 - n))) & 0xFF

target1 = [101, 110, 100, 101, 114, 109, 97, 110]  # "enderman"
target2 = [114, 101, 100, 115, 116, 111, 110, 101]  # "redstone"

p = process('./public/redstone', stdin=PIPE, stdout=PIPE)

line = p.recvline().decode()
iv = int(re.search(r'0x([0-9A-Fa-f]+)', line).group(1), 16)

input1 = []
prev = iv
for t in target1:
    for x in range(256):
        if ROL8(x, 3) ^ prev == t:
            input1.append(x)
            break
    prev = t

p.sendlineafter(b'Enter the code: ', bytes(input1))
log.success(p.recvline().decode().strip())
p.recvline()

iv2 = 0
for b in input1:
    iv2 ^= b

input2 = []
prev = iv2
for i, t in enumerate(target2):
    rot = input1[i] % 8
    for x in range(256):
        if ROL8(x, rot) ^ prev == t:
            input2.append(x)
            break
    prev = t

p.sendlineafter(b'Enter the code: ', bytes(input2))
log.success(p.recvline().decode().strip())
