
Voici les fonctions :

\begin{lstlisting}
def pgcd(a,b): # Calcul du pgcd de a et b
    while b!=0:
        a,b=b,a%b
    return a
def chiffrementAffine(a,b,L): # fonction de chiffrement affine
        if (pgcd(a,26)==1):
                alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
                x=alphabet.index(L)
                y=(a*x+b)%26
                return alphabet[y]
        else:
                return "Chiffrement impossible."
def inverse(a): # Calcul de l'inverse d'un nombre modulo 26
        x=0
        while (a*x%26!=1):
                x=x+1
        return x
def dechiffrementAffine(a,b,L): # Fonction de déchiffrement
        if (pgcd(a,b)==1):
                alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
                x=alphabet.index(L)
                y=(inverse(a)*(x-b))%26
                return alphabet[y]
        else:
                return "Déchiffrement impossible."             
def crypt(M,a,b): # Affichage du mot chiffré
        mot = []
        for i in range(0,len(M)):
                mot.append(chiffrementAffine(a,b,M[i]))
        print ("".join(mot))
def decrypt(M,a,b): # Affichage du mot déchiffré
        mot = []
        for i in range(0,len(M)):
                mot.append(dechiffrementAffine(a,b,M[i]))
        print ("".join(mot))
\end{lstlisting}

En tapant : \texttt{crypt("MATHS",15,8)}, on voit : \og GIHJ \fg{} et en tapant \texttt{decrypt("MIMVC",5,8)}, on voit : \og GAGNE \fg.

\newpage
