39 lines
837 B
Python
39 lines
837 B
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
"""Chiffrement affine"""
|
||
|
|
||
|
from inverse import inverser
|
||
|
|
||
|
|
||
|
def main():
|
||
|
alphabet = input("Alphabet pour le chiffrement affine, terminez par Entrée : ")
|
||
|
kc = 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
|
||
|
inv_a = inverser(a)
|
||
|
op_b = opposer(b)
|
||
|
kd = (inv_a, (inv_a * op_b) % n)
|
||
|
|
||
|
message = input("Message à chiffrer : ")
|
||
|
|
||
|
c = chiffrer(message, alphabet, kc)
|
||
|
print(c)
|
||
|
|
||
|
|
||
|
def chiffrer(message: str, alphabet: str, cle: tuple[int, int]):
|
||
|
n = len(alphabet)
|
||
|
a, b = cle
|
||
|
output = ""
|
||
|
for symbol in message:
|
||
|
m = list(alphabet).index(symbol)
|
||
|
c = (a * m + b) % n
|
||
|
output += alphabet[c]
|
||
|
|
||
|
return output
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|