def grille(a, b):
return chr(65 + (ord(a) + ord(b)) % 26)
def invGrille(a, b):
return chr(65 + (ord(a) - ord(b)) % 26)
def chiffreAutoclaveV1(phrase:str, cle:str):
phrase = phrase.upper()
chiffre = ''
for c in phrase:
cle = grille(c, cle)
chiffre += cle
return chiffre
def dechiffreAutoclaveV1(chiffre:str, cle:str):
clair = ''
for c in chiffre:
clair += invGrille(c, cle)
cle = c
return clair
phrase = 'OPERATIONURGENTE'
cle = 'K'
chiffre = chiffreAutoclaveV1(phrase, cle)
print('Chiffrée :', chiffre)
print('Déchiffrée :', dechiffreAutoclaveV1(chiffre, cle))
def chiffreAutoclaveV2(phrase:str, mot:str):
phrase = phrase.upper()
cle = mot.upper() + phrase
chiffre = ''
for i, c in enumerate(phrase): chiffre += grille(c, cle[i])
return chiffre
def dechiffreAutoclaveV2(chiffre:str, mot:str):
clair = ''
cle = mot
for i, c in enumerate(chiffre):
lettre = invGrille(c, cle[i])
clair += lettre
cle += lettre
return clair
phrase = 'MESSAGEIMPORTANTPOURNESTORLAGIRAFEAUNLONGCOU'
mot = 'POIRIER'
chiffre = chiffreAutoclaveV2(phrase, mot)
print('Chiffrée :', chiffre)
print('Déchiffrée :', dechiffreAutoclaveV2(chiffre, mot))