> For the complete documentation index, see [llms.txt](https://writeups.ayweth20.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://writeups.ayweth20.com/2025/404ctf-2025/cryptanalyse/message-lointain.md).

# Message lointain

**Catégorie:** Cryptanalyse - **Difficulté:** Intro

{% file src="/files/USuetTm88gvHoYR4Slzb" %}

**Description:**

<figure><img src="https://4219205392-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fybn4btwQnvitOl9uXz9p%2Fuploads%2FHM0tmUvzvyyWfyur5mSm%2Fimage.png?alt=media&amp;token=591d6c81-35e3-4295-b0d8-c67a387fb80c" alt=""><figcaption></figcaption></figure>

Solution:

Ici il s'agit d'un chall très simple où le flag a été chiffré avec la fonction `encrypt` :&#x20;

<figure><img src="https://4219205392-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fybn4btwQnvitOl9uXz9p%2Fuploads%2FO84Jz2ML0aISkdOPWxm8%2Fimage.png?alt=media&amp;token=68c218a6-c466-45a1-96ba-49bcbd739531" alt=""><figcaption></figcaption></figure>

Le but ici est de créer une fonction decrypt afin de lui donner notre flag et retrouver l'original.

Voici les étapes à suivre :&#x20;

* Pour chaque caractère chiffré, retrouver son indice `y` dans `charset`.
* Retrouver l'entier `x` tel que `pow(2, x, n+1) == y`.
* En déduire le caractère clair d’indice `x` dans `charset`.

Avec ces infos, la création de la fonction est très rapide :&#x20;

```python
inverse_pow2 = {}
for x in range(n):
    y = pow(2, x, n+1)
    inverse_pow2[y] = x  # y est l'indice chiffré, x est l'indice original

def decrypt(ciphertext):
    decrypted = []
    for char in ciphertext:
        if char in charset:
            y = charset.index(char)
            if y in inverse_pow2:
                x = inverse_pow2[y]
                decrypted.append(charset[x])
            else:
                # Cas rare si y ne correspond à aucun pow(2, x, n+1)
                decrypted.append('?')
    return ''.join(decrypted)

print("DECRYPTED FLAG : ", decrypt("828x6Yvx2sOnzMM4nI2sQ"))
```

Et voilà le résultat :&#x20;

<figure><img src="https://4219205392-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fybn4btwQnvitOl9uXz9p%2Fuploads%2Fm9OTgN3qEiNwV8U3Yfj6%2Fimage.png?alt=media&amp;token=7eed0109-0321-485e-8005-326c492f0348" alt=""><figcaption></figcaption></figure>

<details>

<summary>🚩FLAG</summary>

`404CTF{C0nstEllAt!0n}`

</details>
