> 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/general-skills/pw-crack-3.md).

# PW Crack 3

<img src="https://github.com/user-attachments/assets/38110c96-41bb-45bf-8613-60b421e8a172" alt="image" height="392" width="1083">

```
enc = open("level3.flag.txt.enc", "rb").read()
```

You open the encrypted file in **binary mode (`rb`)** and read all its contents into the variable `enc`. This gives you raw bytes instead of text, which is important because encryption works at the byte level.

```
known = b"picoCTF{"
```

Here you define a **known plaintext prefix**. PicoCTF flags always start with `picoCTF{`, so you’re using that as a reference to recover the key.

```
key = bytes([a ^ b for a, b in zip(enc, known)])
```

This is the key step. You:

* Pair each byte of the ciphertext (`enc`) with each byte of the known plaintext (`known`) using `zip`
* XOR (`^`) them together

Since encryption was likely done as:

```
cipher = plaintext ^ key
```

You reverse it as:

```
key = cipher ^ plaintext
```

This gives you the **first part of the repeating XOR key**, which turns out to be:

```
b'22952295'
```

```
print(key)
```

Just prints the recovered key so you can see it.

```
decrypted = bytes([c ^ key[i % len(key)] for i, c in enumerate(enc)])
```

Now you decrypt the entire ciphertext:

* `enumerate(enc)` gives you both the index `i` and byte `c`
* `key[i % len(key)]` repeats the key cyclically
* Each ciphertext byte is XORed with the corresponding key byte

This reconstructs the original plaintext.

```
print(decrypted.decode())
```

Finally, you convert the decrypted bytes into a readable string using `.decode()` and print it.

```
picoCTF{m45h_fl1ng1ng_6f98a49f}
```


---

# 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/general-skills/pw-crack-3.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.
