chiffrementaffine.py

Created by jerem-jarsaillon

Created on March 02, 2024

1.18 KB


# Programme de décodage de chiffrement affine sur la calculatrice NumWorks

# Fonction pour calculer l'inverse multiplicatif modulo n
def inverse_modulo(a, n):
    for x in range(1, n):
        if (a * x) % n == 1:
            return x
    return None

# Fonction pour décoder le chiffrement affine
def decode_affine(ciphertext, a, b):
    decrypted_text = ""
    inverse_a = inverse_modulo(a, 26)
    if inverse_a is None:
        return "Impossible de décoder avec cette clé."
    else:
        for char in ciphertext:
            if char.isalpha():
                char_num = ord(char) - ord('A')
                decrypted_num = (inverse_a * (char_num - b)) % 26
                decrypted_text += chr(decrypted_num + ord('A'))
            else:
                decrypted_text += char
        return decrypted_text

# Entrée de l'utilisateur pour le message chiffré et la clé
ciphertext = input("Entrez le message chiffré : ").upper()
a = int(input("Entrez la valeur de a : "))
b = int(input("Entrez la valeur de b : "))

# Décodage et affichage du message déchiffré
decrypted_message = decode_affine(ciphertext, a, b)
print("Message déchiffré :", decrypted_message)

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.