Skip to content

Instantly share code, notes, and snippets.

@zar3bski
Created August 17, 2024 10:38
Show Gist options
  • Save zar3bski/757ef0259aff2f775ac22220c2763b59 to your computer and use it in GitHub Desktop.
Save zar3bski/757ef0259aff2f775ac22220c2763b59 to your computer and use it in GitHub Desktop.

Soit A, B, C tel que A XOR B = C. La fonction inversive de XOR est également un XOR => A XOR C = B. Autrement dit, si on a c (le message chiffré) et A (la clef), appliquer XOR permet de retrouver B (le message en clair). Problème nous n'avons pas la clef. En revanche, nous savons qu'elle consiste en une répétition d'une chaine de 4 bytes

key = os.urandom(4) * 20

et nous connaissons les 4 premiers caractères du message en clair: FCSC. La solution consiste alors à chercher une combinaison de bytes tels que <combinaison_de_bytes> XOR <debut_du message_chiffre> = FCSC. La répétition 20 de cette combinaison est notre key. L'utiliser pour déchiffrer le message.

from Crypto.Util.strxor import strxor

with open("output.txt") as file:
    c = bytes.fromhex(file.read())
    clair = b"FCSC"
    seed = []
    for l in range(4):
        for i in range(0xFF):
            b = bytes([i])
            if strxor(c[l : l + 1], b) == clair[l : l + 1]:
                seed.append(bytes(b))
    seed = b"".join(seed)
    key = seed * 20
    print(strxor(key[: len(c)], c).decode("utf-8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment