> For the complete documentation index, see [llms.txt](https://thr34dr1pp3r.gitbook.io/ctf/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thr34dr1pp3r.gitbook.io/ctf/uiuctf-2024/crypto-determined.md).

# Crypto-Determined

## [Full Implementation](https://github.com/kinasant/THR34DR1PP3R/blob/main/2024/UIUCTF/Determined/sol.ipynb)

## Challenge

<pre class="language-python"><code class="lang-python"><strong>def inputs():
</strong>    print("[DET] First things first, gimme some numbers:")
    M = [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
    ]
    try:
        M[0][0] = p
        M[0][2] = int(input("[DET] M[0][2] = "))
        M[0][4] = int(input("[DET] M[0][4] = "))

        M[1][1] = int(input("[DET] M[1][1] = "))
        M[1][3] = int(input("[DET] M[1][3] = "))

        M[2][0] = int(input("[DET] M[2][0] = "))
        M[2][2] = int(input("[DET] M[2][2] = "))
        M[2][4] = int(input("[DET] M[2][4] = "))

        M[3][1] = q
        M[3][3] = r

        M[4][0] = int(input("[DET] M[4][0] = "))
        M[4][2] = int(input("[DET] M[4][2] = "))
    except:
        return None
    return M
def main():
    print("[DET] Welcome")
    M = inputs()
    if M is None:
        print("[DET] You tried something weird...")
        return
    res = fun(M)
    print(f"[DET] Have fun: {res}")

</code></pre>

It is similar to the [previous question](/ctf/uiuctf-2024/crypto-without-a-trace.md) except instead of trace, they give us the determinant of the matrix with p and q values encoded

Let `i1,i2,i3,i4,i5,i6,i7,i8,i9` be the inputs. Then the matrix is&#x20;

```python
[ p  0 i1  0 i2]
[ 0 i3  0 i4  0]
[i5  0 i6  0 i7]
[ 0  q  0  r  0]
[i8  0 i9  0  0]
```

We calculate the determinant under the symbolic ring to get a direct equation for the determinant

```python
P.<i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,p,q,r> = PolynomialRing(SR)
M = Matrix([[p,0,i1,0,i2],[0,i3,0,i4,0],[i5,0,i6,0,i7],[0,q,0,r,0],[i8,0,i9,0,0]])
det(M)
i2*i4*i6*i8*q - i1*i4*i7*i8*q - i2*i4*i5*i9*q + i4*i7*i9*p*q - i2*i3*i6*i8*r + i1*i3*i7*i8*r + i2*i3*i5*i9*r - i3*i7*i9*p*r
```

We can solve for q by setting  `(i2*i4*i6*i8) = 1` and setting the rest to 0

On solving for q, we can decrypt the flag

```python
from Crypto.Util.number import *
q = 12538849920148192679761652092105069246209474199128389797086660026385076472391166777813291228233723977872612370392782047693930516418386543325438897742835129
n = 158794636700752922781275926476194117856757725604680390949164778150869764326023702391967976086363365534718230514141547968577753309521188288428236024251993839560087229636799779157903650823700424848036276986652311165197569877428810358366358203174595667453056843209344115949077094799081260298678936223331932826351
c = 72186625991702159773441286864850566837138114624570350089877959520356759693054091827950124758916323653021925443200239303328819702117245200182521971965172749321771266746783797202515535351816124885833031875091162736190721470393029924557370228547165074694258453101355875242872797209141366404264775972151904835111
e = 65535
p = n//q
d = inverse(e,(p-1)*(q-1))
print(long_to_bytes(pow(c,d,n)))
b'uiuctf{h4rd_w0rk_&&_d3t3rm1n4t10n}'
```


---

# 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:

```
GET https://thr34dr1pp3r.gitbook.io/ctf/uiuctf-2024/crypto-determined.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
