Break My Stream - cryptography - cryptopia

Writeup - by bylal

This is the script that solved:

from pwn import *

def xor(a, b):
    return bytes([x ^ y for x, y in zip(a, b)])

conn = remote("0.cloud.chals.io", 31561)

# Read intro and get encrypted flag
line = conn.recvuntil(b"thing: ")
flag_ct_hex = conn.recvline().strip()
flag_ct = bytes.fromhex(flag_ct_hex.decode())
print("[+] Got encrypted flag:", flag_ct.hex())

# Send known plaintext of same length
known_pt = b'A' * len(flag_ct)
conn.sendlineafter(b"Enter your message: ", known_pt)

# Receive encrypted known plaintext
known_ct_hex = conn.recvline().strip()
known_ct = bytes.fromhex(known_ct_hex.decode())
print("[+] Got encrypted known plaintext:", known_ct.hex())

# Recover keystream
keystream = xor(known_ct, known_pt)

# Decrypt the flag
flag = xor(flag_ct, keystream)
print("[+] Recovered flag:", flag.decode())