
Q7 — Chiffrement
Algorithme :
Algorithme Chiffres_Base(n, b) : Liste Variables : res : Liste Début Si n = 0 Alors Retourner [0]
res ← [] Tant que n > 0 Faire Insérer (n mod b) en tête de res n ← n div b FinTantQue Retourner
res Fin Algorithme Crypter_Mot(mot, b, K) : Chaîne Variables : c, code : Entier ; chiffres, inv :
Listes ; res : Chaîne Début res ← "" Pour chaque caractère c dans mot Faire code ← ASCII(c)
chiffres ← Chiffres_Base(code, b) inv ← Inverser(chiffres) code ← BaseVersDécimal(inv, b) code
← code + K res ← res + Caractère(code) FinPour Retourner res Fin
Code Python :
def chiffres_base(n, b): if n == 0: return [0] res = [] while n > 0: res.insert(0, n % b) n //= b
return res def base_vers_decimal(chiffres, b): val = 0 for d in chiffres: val = val * b + d
return val def crypter_mot(mot, b, K): crypte = "" for c in mot: code = ord(c) chiffres =
chiffres_base(code, b) inv = chiffres[::-1] code = base_vers_decimal(inv, b) code += K crypte +=
chr(code) return crypte mots_cryptes = [] for mot in mots_selec: c = crypter_mot(mot, b, K)
mots_cryptes.append(c) print(f"{mot} → {c}")
# Sortie (b=2, K=5) : # RADAR → (mot crypté correspondant) # BONJOUR → (mot crypté
correspondant) # LEVEL → (mot crypté correspondant) # Chaque caractère : ASCII → base 2 →
inversion → décimal → +5 → chr
Q8 — Matrice des mots cryptés
Algorithme :
Algorithme Matrice_Cryptee(mots_cryptes, p) Variables : tries : Liste ; k, i, j : Entier mat_c :
Tableau p×p Début tries ← Trier(mots_cryptes) Créer mat_c p×p remplie de '*' k ← 0 Pour i ← 0 à
p-1 Faire Pour j ← 0 à p-1 Faire Si k < Taille(tries) Alors mat_c[i][j] ← tries[k] ; k ← k + 1
FinSi FinPour FinPour Afficher mat_c Retourner mat_c Fin
Code Python :
def matrice_cryptee(mots_cryptes, p): tries = sorted(mots_cryptes) mat_c = [['*'] * p for _ in
range(p)] k = 0 for i in range(p): for j in range(p): if k < len(tries): mat_c[i][j] = tries[k] k
+= 1 for ligne in mat_c: print(ligne) return mat_c mat_c = matrice_cryptee(mots_cryptes, p)
# Sortie : matrice p×p avec les mots cryptés triés # lexicographiquement, cases restantes à
'*'
Q9 — Sauvegarde dans resultat.dat
Algorithme :
Algorithme Sauvegarder(K, mots_cryptes, mat_c) Début Ouvrir "resultat.dat" en écriture binaire
Écrire_Pickle({ 'K': K, 'mots_cryptes': mots_cryptes, 'matrice': mat_c }) Fermer fichier
Afficher "Sauvegarde OK" Fin
Code Python :
import pickle def sauvegarder(K, mots_cryptes, mat_c, nom='resultat.dat'): donnees = {'K': K,
'mots_cryptes': mots_cryptes, 'matrice': mat_c} with open(nom, 'wb') as f: pickle.dump(donnees,
f) print(f"Sauvegarde OK dans '{nom}'") sauvegarder(K, mots_cryptes, mat_c)
Q10 — Relecture et vérification
Algorithme :
Algorithme Relire_Verifier(mots_cryptes_ref) Variables : donnees : Dictionnaire Début Ouvrir
"resultat.dat" en lecture binaire donnees ← Lire_Pickle() Fermer fichier Afficher "K :",
donnees['K'] Afficher "Mots cryptés :", donnees['mots_cryptes'] Afficher "Matrice :",
donnees['matrice'] Si donnees['mots_cryptes'] = mots_cryptes_ref Alors Afficher "OK" Sinon
Afficher "ERREUR" FinSi Fin
Code Python :
def relire_verifier(mots_cryptes_ref, nom='resultat.dat'): with open(nom, 'rb') as f: donnees =
pickle.load(f) print("=== Contenu de resultat.dat ===") print("K :", donnees['K']) print("Mots
cryptés :", donnees['mots_cryptes']) print("Matrice :") for ligne in donnees['matrice']: print("
", ligne) # Vérification statut = "OK" if donnees['mots_cryptes'] == mots_cryptes_ref else