Add déchiffrement to chiffrement_affine
This commit is contained in:
parent
d901762558
commit
ea4049863f
1 changed files with 37 additions and 7 deletions
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue