Загрузка данных


import socket
import json
import re

HOST = 'tasks.duckerz.ru'
PORT = 30009

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(30.0)
s.connect((HOST, PORT))

data = b""
try:
    while b"Send to Bob:" not in data:
        chunk = s.recv(4096)
        if not chunk:
            break
        data += chunk
except socket.timeout:
    pass

text = data.decode('utf-8', errors='ignore')
match = re.search(r'\{.*\}', text)
alice = json.loads(match.group())

p = int(alice['p'], 16)
g = int(alice['g'], 16)
A = int(alice['A'], 16)

print(f"[*] p = {p} ({p.bit_length()} бит)")
print(f"[*] g = {g}")
print(f"[*] A = {A}")

# Перебираем a и b от 1 до 100
print("[*] Перебираем a и b от 1 до 100...")
found = None
for a in range(1, 101):
    for b in range(1, 101):
        if pow(g, a + b, p) == A:
            found = (a, b, a + b)
            print(f"[+] НАЙДЕНО! a={a}, b={b}, x={a+b}")
            break
    if found:
        break

if found:
    a, b, x = found
    payload = json.dumps({"answer": x})
    s.sendall((payload + "\n").encode())
    resp = s.recv(4096).decode('utf-8', errors='ignore')
    print(f"[+] Ответ: {resp}")
else:
    print("[-] Не найдено")
    # Пробуем отправить A
    payload = json.dumps({"answer": A})
    s.sendall((payload + "\n").encode())
    resp = s.recv(4096).decode('utf-8', errors='ignore')
    print(f"[+] Ответ: {resp}")

s.close()