Unsigned DH: man-in-the-middle
Diffie-Hellman is perfectly protected against discrete logarithms, but it has an inherent vulnerability: no one authenticates themselves in the protocol. Alice received B — but she does not know if it really came from Bob.
Mallory stands in the middle of the channel and sends M = g^m mod p with her private number m to both parties. As a result, two separate sessions emerge:
- Alice ↔ Mallory:
s₁ = A^m mod p = M^a mod p - Mallory ↔ Bob:
s₂ = B^m mod p = M^b mod p
Neither Alice nor Bob notices anything: each generated a "shared secret" and encrypted the message. Mallory, meanwhile, reads in the middle and modifies it if necessary. This is exactly why in real TLS DH parameters are signed.
Mallory's private number m was found on their laptop, and the network capture is in the tinglangan.txt file. Read Alice's message.
Encryption method (on Alice's side):
import hashlib
def kdf(s: int) -> bytes:
return hashlib.sha256(str(s).encode()).digest() # 32 bayt
kalit = kdf(s1)
shifr = bytes(c ^ kalit[i % 32] for i, c in enumerate(xabar))