crypto2.py

Created by dan-wallez

Created on August 08, 2020

3.7 KB

Pour plus de confort paramétrer police python à petit dans paramètres. Fonction codage_affine() saisir les paramètres a_c et b_c de la fonction affine de codage, le programme détermine les paramètres a_d et b_d de la fonction affine décodage. Seuls les lettres et espaces sont acceptés. Fonction codage_cesar() : saisir le paramètre de décalage ainsi que le mot en MAJ à coder. Fonction codage_vigenere() : Saisir le mot clé de codage ainsi que le mot à coder (tout en MAJ).


from math import *



def gcdw(a,b):

    a = int(fabs(a))

    b = int(fabs(b))

    r = 1

    if a % b == 0 :

        return b

    else :

        while r > 0 :

            r = a % b

            a = b

            b = r

        return a

    

def bezout_aff(a, b):

    if b == 0 :

        u = int(abs(a) / a)

    else :

        r, u, v, x, y = 1, 1, 0, 0, 1

        while r > 0:

            q = a // b

            if b < 0 and q != a / b:

                q += 1

            r = a - (b * q)

            a, b = b, r

            s = u - (x * q)

            u, x = x, s

            t = v - (q * y)

            v, y = y, t

    return (u)



def cod_aff(mot, a, b):

    mot_code = ""

    i = 0

    while i < len(mot):

        x = ord(mot[i]) - ord("A")

        if x == ord(" ") - ord("A") :

            mot_code = mot_code + " "

        else :

            y = a * x + b

            while y < 0:

                y += 26

            while y >= 26:

                y -= 26

            y += ord("A")

            mot_code = mot_code + chr(y)

        i += 1

    return (mot_code)



def cod_inv_aff(a, b):

    u = bezout_aff(a, -26)

    a_x = u

    while a_x < 0:

        a_x += 26

    b_x = -(u * b)

    while b_x >= 26:

        b_x -= 26

    while b_x < 0:

        b_x += 26

    return(a_x, b_x)



def cod_ces(mot, k):

    mot_code = ""

    i = 0

    while i < len(mot):

        x = ord(mot[i]) - ord("A")

        xcode = (x+k) % 26

        mot_code += chr(xcode + ord("A"))

        i += 1

    return (mot_code)



def cod_vigenere(mot, mot_cle):

    mot_code = ""

    k = len(mot_cle)

    i = 0

    while i < len(mot):

        xcode = (ord(mot[i]) + ord(mot_cle[i%k]) - 2*ord("A")) % 26

        mot_code += chr(xcode + ord("A"))

        i += 1

    return (mot_code)



def decod_vigenere(mot, mot_cle):

    mot_decode = ""

    k = len(mot_cle)

    i = 0

    while i < len(mot):

        xcode = (ord(mot[i]) - ord(mot_cle[i%k])) % 26

        mot_decode += chr(xcode + ord("A"))

        i += 1

    return (mot_decode)



   

#  PROGRAMMES PRINCIPAUX





#   CODAGE AFFINE

#   lettres majuscules et touche espace    



def codage_affine():



    print ("Rappel : fonction affine = ax + b")

    print ("Saisie pour le codage de a_c et b_c")

    print ("IMPORTANT : a_c et 26 premiers entre eux\n")

    

    a_c = int(input("a_c = "))

    b_c = int(input("b_c = "))



    pgcd = gcdw(a_c, 26)

    if pgcd > 1 :

        print ("\nPGCD de",a_c,"et 26 =", pgcd, "> 1 codage affine impossible")

    else:

        print ("Mot (MAJ ou espace):")

        mot_a_coder = input()

        mot_code = cod_aff(mot_a_coder, a_c, b_c)

        print ("\nMot(s) code(s) :")

        print (mot_code)

        

        print ("\nDétermination pour décodage")

        print ("de a_d et b_d\n")

        a_d, b_d = cod_inv_aff(a_c, b_c)

        print ("a_d =", a_d)

        print ("b_d =", b_d)



        mot_origine = cod_aff(mot_code, a_d, b_d)

        print ("\nMot(s) origine(s) :")

        print (mot_origine)



#    CODAGE DE CESAR   

#    uniqmnt lettres majuscules



def codage_cesar() :



    k = int(input("valeur du décalage = "))  # ou cle de Cesar

    mot = input("Mot (en MAJ) : ")

    mot_code = cod_ces(mot, k)

    print ("Mot code : ", mot_code)

    mot_origine = cod_ces(mot_code, -k)

    print ("Mot origine : ", mot_origine)



#    CODAGE DE VIGENERE

#    uniqmnt lettres majuscules



def codage_vigenere():

    mot = input("Mot (en MAJ) : ")

    mot_cle = input("Mot cle (en MAJ) = ")



    mot_code = cod_vigenere(mot, mot_cle)

    print ("Mot code : ", mot_code)



    mot_origine = decod_vigenere(mot_code, mot_cle)

    print ("Mot origine : ", mot_origine)



    

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.