Advertisement
Hellerick_Ferlibay

Caesar's cipher

Sep 10th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. print("""== Caesar's encryption/decryption algorythm ==
  2. To switch between encryption/decryption modes, enter a single 'e' or 'd'.
  3. To input encryption key, enter a number 1...25.
  4. To quit, enter 0.""")
  5.  
  6. def cipher(t):
  7.     d = {chr(ord('a')+i):chr(ord('a')+(i+key*encrypt)%26) for i in range(26)}
  8.     d.update({chr(ord('A')+i):chr(ord('A')+(i+key*encrypt)%26) for i in range(26)})
  9.     return ''.join([d[c] if c.isalpha() else c for c in t])
  10.  
  11. go_on = True
  12. encrypt = 1
  13. key = 0
  14.  
  15. while go_on:
  16.     u = input()
  17.     if u in [str(i+1) for i in range(25)]:
  18.         key = int(u)
  19.         print('<Encryption key switched to %d>'%key)
  20.     elif u == '0':
  21.         go_on = False
  22.         print('<Quit>')
  23.     elif u == 'e':
  24.         encrypt = 1
  25.         print('<Switched to encryption mode>')
  26.     elif u == 'd':
  27.         encrypt = -1
  28.         print('<Switched to decryption mode>')
  29.     else:
  30.         print(cipher(u))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement