1+ import base64
2+ import os
13from ..detecteur_crypto import CryptoAnalyzer
24from ..utils import calculer_entropie
35import hashlib
4- from cryptography .hazmat .primitives .ciphers import algorithms , Cipher , modes
6+ import re
7+ from cryptography .hazmat .primitives .ciphers import Cipher , modes
58from cryptography .hazmat .primitives .padding import PKCS7
9+ from cryptography .hazmat .decrepit .ciphers .algorithms import Blowfish
610class Blowfish_Analyzer (CryptoAnalyzer ):
711 '''Détermine si l'algo blowfish est utilisé, génère des clés et tente de de déchffrer un fichier chiffré en utilisant les clés générées.
812
@@ -17,7 +21,7 @@ class Blowfish_Analyzer(CryptoAnalyzer):
1721
1822 '''
1923
20- __BLOWFISH_TAILLE_BLOC = 8
24+ __BLOWFISH_TAILLE_BLOC = 64
2125 __BLOWFISH_TAILLE_IV = 8
2226
2327 def identifier_algo (self , chemin_fichier_chiffre : str ) -> float :
@@ -132,6 +136,18 @@ def generer_cles_candidates(self, chemin_dictionnaire: str) -> list[bytes]:
132136
133137 return cles_candidates
134138
139+ def decode_base64 (self , encoded_bytes , altchars = b'+/' ):
140+ encoded_bytes = re .sub (
141+ rb'[^a-zA-Z0-9%s]+' %
142+ altchars , b'' , encoded_bytes )
143+
144+ missing_padding_length = len (encoded_bytes ) % 4
145+
146+ if missing_padding_length :
147+ encoded_bytes += b'=' * (4 - missing_padding_length )
148+
149+ return base64 .b64decode (encoded_bytes , altchars )
150+
135151 def dechiffrer (self , chemin_fichier_chiffre : str , cle_donnee : bytes ) -> bytes :
136152 """
137153 Déchiffre le fichier supposé crypté par l'algorithme blowfish avec la clé donnée en respectant les critères de
@@ -146,19 +162,22 @@ def dechiffrer(self, chemin_fichier_chiffre: str, cle_donnee: bytes) -> bytes:
146162 """
147163
148164 #La taille de clé est dans l'intervalle 32-448bits et est multiple de 8
149- if len (cle_donnee ) not in range (32 , 448 , 8 ):
165+ print (cle_donnee )
166+ if len (cle_donnee ) not in range (4 , 55 , 8 ):
167+ print (len (cle_donnee ))
150168 raise ValueError ('Taille de clé invalide.' )
151169
152170 try :
153-
154- algorithm_blowfish = algorithms . Blowfish (cle_donnee )
171+
172+ algorithm_blowfish = Blowfish (self . decode_base64 ( cle_donnee ) )
155173 texte_chiffre = ''
156174
157- #Récupération de l'IV et des texte chiffré das le fichier
175+ #Récupération de l'IV et du texte chiffré dans le fichier
158176 with open (chemin_fichier_chiffre , 'rb' ) as f :
159- initialization_vector = f .read (self .__BLOWFISH_TAILLE_IV )
160- texte_chiffre = f .read ()
177+ donnees = f .read ()
161178 f .close ()
179+ initialization_vector = donnees [:self .__BLOWFISH_TAILLE_IV ]
180+ texte_chiffre = donnees [self .__BLOWFISH_TAILLE_IV :]
162181
163182 #Initialisation du cipher
164183 cipher = Cipher (algorithm_blowfish , modes .CBC (initialization_vector ))
@@ -178,3 +197,11 @@ def dechiffrer(self, chemin_fichier_chiffre: str, cle_donnee: bytes) -> bytes:
178197 raise
179198
180199
200+ # if __name__ == "__main__":
201+ # try:
202+ # resultat_dechiffrement: bytes = Blowfish_Analyzer().dechiffrer("CryptoForensic-Python/data/mission3.enc", os.urandom(32))
203+ # print(f"Résultat du déchiffrement : {resultat_dechiffrement.decode('utf-8')}")
204+ # except ValueError as ve:
205+ # print(ve)
206+ # except FileNotFoundError:
207+ # print("Erreur: Le fichier 'mission2.enc' est introuvable.")
0 commit comments