From ea4049863f8cd9231646d366e36ddfe5c2c47f30 Mon Sep 17 00:00:00 2001 From: "flyingscorpio@pinebookpro" Date: Tue, 25 Jan 2022 10:52:19 +0100 Subject: [PATCH] =?UTF-8?q?Add=20d=C3=A9chiffrement=20to=20chiffrement=5Fa?= =?UTF-8?q?ffine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algebre-non-lineaire/chiffrement_affine.py | 44 ++++++++++++++++++---- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/algebre-non-lineaire/chiffrement_affine.py b/algebre-non-lineaire/chiffrement_affine.py index 2f0c151..2820908 100755 --- a/algebre-non-lineaire/chiffrement_affine.py +++ b/algebre-non-lineaire/chiffrement_affine.py @@ -2,24 +2,41 @@ """Chiffrement affine""" +import sys + from inverse import inverser +from oppose import opposer def main(): + if len(sys.argv) == 2: + if sys.argv[1] == "chiffrer": + action = "chiffrer" + elif sys.argv[1] == "déchiffrer": + action = "déchiffrer" + else: + action = input("Chiffrer ou déchiffrer ? ") + alphabet = input("Alphabet pour le chiffrement affine, terminez par Entrée : ") - kc = input( + a, b = input( "Clé de chiffrement (deux nombres séparés par un espace, exemple: 2 4) " ).split(" ")[:2] - kc = tuple(int(i) for i in kc) - a, b = kc + kc = (int(a), int(b)) inv_a = inverser(a) + if not inv_a: + print(f"{a} doit être inversible") + return op_b = opposer(b) kd = (inv_a, (inv_a * op_b) % n) - message = input("Message à chiffrer : ") - - c = chiffrer(message, alphabet, kc) - print(c) + if action == "chiffrer": + message = input("Message à chiffrer : ") + c = chiffrer(message, alphabet, kc) + print(c) + elif action == "déchiffrer": + message = input("Message à déchiffrer : ") + m = dechiffrer(message, alphabet, kd) + print(m) def chiffrer(message: str, alphabet: str, cle: tuple[int, int]): @@ -34,5 +51,18 @@ def chiffrer(message: str, alphabet: str, cle: tuple[int, int]): return output +def dechiffrer(message: str, alphabet: str, cle: tuple[int, int]): + n = len(alphabet) + a, b = cle + output = "" + for symbol in message: + c = list(alphabet).index(symbol) + m = (a * c + a * b) % n + output += alphabet[m] + + return output + + + if __name__ == "__main__": main()