> For the complete documentation index, see [llms.txt](https://sarpers-organization.gitbook.io/ctftricks/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sarpers-organization.gitbook.io/ctftricks/_chapter-intro-3/key-exchange/cryptography-key-exchange-diffie-hellman.md).

# Diffie Hellman

## Diffie-Hellman Calculation For Path Discovery

```python
p = 9975298661930085086019708402870402191114171745913160469454315876556947370642799226714405016920875594030192024506376929926694545081888689821796050434591251
g = 7
a = 330
b = 450
gc = 6091917800833598741530924081762225477418277010142022622731688158297759621329407070985497917078988791448889947074350694220209769840915705739528359582454617

op1 = pow(gc, a, p) # Calculate (g^c)^a mod p
result = pow(op1, b, p) # Calculate ((g^c)^a)^b mod p

print(str(result)[:128])
```

Given Diffie-Hellman public parameters ($p$, $g$), a value $gc$ which is $g^c \pmod p$, and exponents $a$ and $b$, you can compute a derived secret key by calculating $((g^c)^a)^b \pmod p$, which simplifies to $g^{cab} \pmod p$. The provided Python code performs this modular exponentiation using `pow(base, exp, mod)` to efficiently compute $(gc^a)^b \pmod p$. The first 128 digits of the resulting integer are then used as a potential hidden directory path or key.
