Advertisement
Vincent38190

cesar.py

May 20th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1.  
  2. alphabet = []
  3. for i in range (0,26):
  4.     alphabet.append(chr(ord('A')+i))
  5. for i in range (0,26):
  6.     alphabet.append(chr(ord('a')+i))
  7. for i in range (0,10):
  8.     alphabet.append(chr(ord('0')+i))
  9. alphabet.append(' ')
  10. alphabet.append('!')
  11.  
  12. noCar = '?'
  13.  
  14. def modulo(num,mod):
  15.     if num > 0:
  16.         while num > mod-1:
  17.             num -= mod
  18.         return num
  19.     else:
  20.         while num < 0:
  21.             num += mod
  22.         return num
  23.  
  24. def AsciiToAlphabet(letter):
  25.         for i in range (0,len(alphabet)):
  26.             if(alphabet[i] == letter):
  27.                 return i
  28.         return -1
  29.            
  30. def AlphabetToAscii(num):
  31.         if(num >= len(alphabet)):
  32.             return noCar
  33.         else:
  34.             return alphabet[num]
  35.  
  36. def Crypt(phrase,passphrase):
  37.     rep = ""
  38.     for i in range (0,len(phrase)):
  39.         rep += AlphabetToAscii(modulo(AsciiToAlphabet(phrase[i]) + AsciiToAlphabet(passphrase[modulo(i,len(passphrase))]),len(alphabet)))
  40.     return rep
  41.  
  42. def Uncrypt(phrase,passphrase):
  43.     rep = ""
  44.     for i in range (0,len(phrase)):
  45.         rep += AlphabetToAscii(modulo(AsciiToAlphabet(phrase[i]) - AsciiToAlphabet(passphrase[modulo(i,len(passphrase))]),len(alphabet)))
  46.     return rep
  47.  
  48. isContinue = True
  49. while isContinue:
  50.     mode = input("Quel mode (d/c/q) : ")
  51.     while mode != "d" and mode != "c" and mode != "q":
  52.         mode = input("Entrer (d/c/q)!\nQuel mode (d/c/q) : ")
  53.     if mode == "q":
  54.         isContinue = False
  55.         continue
  56.     word = input("Entrer mot : ")
  57.     passphrase = input("Entrer passphrase : ")
  58.  
  59.     if mode == "c":
  60.         print(Crypt(word,passphrase))
  61.     else:
  62.         print(Uncrypt(word,passphrase))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement