1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #!/usr/bin/python
# -*- coding: UTF-8 -*-
alphabet = "abcdefghijklmnopqrstuvwxyz"
def Encrypt(text, pas=4):
localalpha = alphabet[pas:] + alphabet[:pas]
result = ""
for c in text:
if c.lower() in alphabet:
if c.lower() == c:
result += localalpha[alphabet.index(c)]
else:
result += localalpha[alphabet.index(c.lower())].upper()
else:
result += c
return result
def Decrypt(text, pas=4):
localalpha = alphabet[pas:] + alphabet[:pas]
result = ""
for c in text:
if c.lower() in alphabet:
if c.lower() == c:
result += alphabet[localalpha.index(c)]
else:
result += alphabet[localalpha.index(c.lower())].upper()
else:
result += c
return result
def Cesar(text, pas=4, method='Encrypt'):
if method not in ('Encrypt', 'Decrypt'):
print("WARNING : method should be 'Encrypt' or 'Decrypt'.")
print("'Encrypt' assumed by default.")
method = 'Encrypt'
if not isinstance(pas, int):
print("WARNING : Invalid pas value.")
print("pas=4 assumed by default.")
pas = 4
localalpha = alphabet[pas:] + alphabet[:pas]
worklist = alphabet
locallist = localalpha
if method == 'Decrypt':
worklist = localalpha
locallist = alphabet
result = ""
for c in text:
if c.lower() in alphabet:
if c.lower() == c:
result += locallist[worklist.index(c)]
else:
result += locallist[worklist.index(c.lower())].upper()
else:
result += c
return result
texttxt = """Les sanglots longs
Des violons
De l'automne
Blessent mon coeur
D'une langueur
Monotone.
Tout suffocant
Et blême, quand
Sonne l'heure,
Je me souviens
Des jours anciens
Et je pleure
Et je m'en vais
Au vent mauvais
Qui m'emporte
Deçà, delà,
Pareil à la
Feuille morte."""
encrypted = Encrypt(texttxt, pas=4)
print("\nEncrypted :", encrypted)
decrypted = Decrypt(encrypted, pas=4)
print("\nDecrypted :", decrypted)
print("\nUse Cesar\n")
encrypted = Cesar(texttxt, pas=10)
print("\nEncrypted :", encrypted)
decrypted = Cesar(encrypted, pas=10, method="Decrypt")
print("\nDecrypted :", decrypted) |
Partager