> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/cryptography/sharedsecrets.md).

# sharedsecrets

The goal was to decrypt a flag that was XOR-encrypted using a shared secret derived from a Diffie-Hellman key exchange. While Diffie-Hellman is mathematically secure against eavesdroppers who only see public values, this specific instance provided us with the **private exponent** of one of the parties.

#### Technical Breakdown

1. **The Parameters:**
   * $g = 2$: The generator.
   * $p$: A large 1048-bit prime number.
   * $A$: The server's public key ($A = g^a \pmod{p}$).
   * $b$: The client's **private key** (This is the "leaked" information).
2. **The Vulnerability:** In a standard exchange, an attacker only knows $g$, $p$, $A$, and $B$. To find the shared secret, the attacker would need to solve the **Discrete Logarithm Problem** to find $a$ or $b$, which is computationally infeasible for large primes. However, since $b$ was explicitly provided in the file, we can calculate the shared secret exactly as the client would.
3. **The Math:** The shared secret is established by raising the other party's public key to your own private power: $$\text{shared} = A^b \pmod{p}$$ Substituting $A$, we see why this works: $$\text{shared} = (g^a)^b \pmod{p} = g^{ab} \pmod{p}$$
4. **Decryption Logic:** The encryption was a simple XOR cipher using the last byte of the shared secret:
   * $\text{key} = \text{shared} \pmod{256}$
   * $\text{flag}\[i] = \text{enc}\[i] \oplus \text{key}$

***

### Solution Script (Python)

```python
# Extracted values from the challenge
p = 2549189574813286838731164...
A = 9854453750409656602869254...
b = 2531748005435027320362428...
enc_hex = "4d545e527e697b465955624e0e5e4f0e49620404050f5b5b580b40"

# Step 1: Reconstruct the shared secret
shared_secret = pow(A, b, p)

# Step 2: Extract the XOR key (the last byte)
key = shared_secret % 256

# Step 3: XOR the encrypted hex back to plaintext
ciphertext = bytes.fromhex(enc_hex)
flag = "".join(chr(byte ^ key) for byte in ciphertext)

print(f"Flag: {flag}")
```

### Key Takeaway

The security of Diffie-Hellman relies entirely on the secrecy of the exponents $a$ and $b$. If either private exponent is exposed (through a leak, a weak random number generator, or side-channel attacks), the entire encrypted communication is compromised.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/cryptography/sharedsecrets.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
