> 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/customencryption.md).

# customencryption

Alright — let’s clean this up properly because your confusion is **100% valid**. Nothing “mystical” happened, just **CTF trickery + mismatch of values**.

I’ll walk you **step-by-step clean, no BS**, so you can repeat this in exams/CTFs.

***

## 🧠 1. What the challenge ACTUALLY is

This is **NOT RSA** (they trolled you).

It’s:

#### 🔐 Step 1: Diffie-Hellman → shared key

#### 🔐 Step 2: XOR encryption (with `"trudeau"`)

#### 🔐 Step 3: Multiply by `(key * 311)`

## 🔍 3. How to solve it PROPERLY (real method)

Since `a` and `b` are unreliable → **ignore them**

***

### ✅ Step 1: Understand formula

From code:

```python
cipher[i] = ord(char) * key * 311
```

***

### ✅ Step 2: Reverse multiplication

```python
char = cipher[i] / (key * 311)
```

***

### ✅ Step 3: Problem

👉 You don’t know `key`

BUT:

* `p = 97`
* So key ∈ \[1, 96]

👉 Small range = brute force 💀

***

## 🚀 4. Final Working Method (ALWAYS WORKS)

### ✔️ Step-by-step logic

1. Try every possible key
2. Divide cipher
3. Convert to characters
4. Undo XOR
5. Reverse string
6. Check for `flag{}` pattern

***

### 💻 Final Script (clean + exam-ready)

```python
cipher = [33588, 276168, 261240, 302292, 343344, 328416, 242580, 85836, 82104, 156744, 0, 309756, 78372, 18660, 253776, 0, 82104, 320952, 3732, 231384, 89568, 100764, 22392, 22392, 63444, 22392, 97032, 190332, 119424, 182868, 97032, 26124, 44784, 63444]

text_key = "trudeau"

for key in range(1, 100):
    try:
        semi = ""

        # Step 1: undo multiplication
        for num in cipher:
            if num == 0:
                semi += "\x00"
            else:
                val = num // (key * 311)
                if val < 0 or val > 255:
                    raise
                semi += chr(val)

        # Step 2: undo XOR
        result = ""
        for i, c in enumerate(semi):
            result += chr(ord(c) ^ ord(text_key[i % len(text_key)]))

        # Step 3: reverse back
        final = result[::-1]

        # Step 4: check flag pattern
        if "{" in final and "}" in final:
            print(f"[+] Key: {key} -> {final}")

    except:
        continue
```

> If key space is small → **BRUTE FORCE FIRST, THINK LATER**

***

## ⚡ Quick Mental Model

| Situation                  | Action      |
| -------------------------- | ----------- |
| Given a, b look sketchy    | Ignore      |
| Output looks semi-readable | Logic issue |
| Output total garbage       | Math wrong  |
| Small modulus (like 97)    | brute force |

***

## Final Truth

Nothing broke.

👉 You just trusted **fake values** instead of the **cipher itself**

That’s a classic CTF trap.


---

# 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/customencryption.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.
