#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p python3 python3Packages.pwntools python3Packages.pycryptodome

from pwn import *
from Crypto.Util.number import long_to_bytes

def decipher(r: tube, ciphertext: bytes) -> str:
    r.recvuntil(b'Give me ciphertext to decrypt: ')
    r.sendline(ciphertext)
    r.recvuntil(b'Here you go: ')
    return r.recvline().strip()

def main():
    addr, port, *_ = "mercury.picoctf.net 33780".split(" ")
    r = remote(addr, int(port))

    r.recvuntil(b'Good Luck!\n\n\n')

    n = int(r.recvline().split(b' ')[1].strip())
    e = int(r.recvline().split(b' ')[1].strip())
    enc = int(r.recvline().split(b' ')[1].strip())
    print(f"{n=}")
    print(f"{e=}")
    print(f"{enc=}")

    padding = 2
    encrypted_padding = pow(padding, e, n)

    payload = (enc * encrypted_padding)
    output = decipher(r, str(payload).encode())
    flag = long_to_bytes(int(output) // padding).decode()
    print(flag)

if __name__ == "__main__":
    main()
