Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. # -*- coding: cp1252 -*-
  2. """
  3.    Caesar cipher
  4.  
  5.    Shifts the letter to be the letter that comes x step after it
  6.    x = the key
  7.    So if the key is 3 A would become D
  8. """
  9.  
  10. #list of all letters from A-Z
  11. alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","Γ₯","Γ€","ΓΆ"]
  12. #list for text to encrypt
  13. norm_li = []
  14. #encrypted list
  15. ectd_li = []
  16. #decrypted list
  17. decr_li = []
  18.  
  19. def encrypt(li, key):
  20.     for i in li:
  21.         norm_li.append(i)
  22.  
  23.     for x in norm_li:
  24.         if x == " ":
  25.                 ectd_li.append("")
  26.         for a in alphabet:
  27.             if x.lower() == a.lower():
  28.                 if x == x.upper():
  29.                     ectd_li.append(alphabet[(len(alphabet)-(len(alphabet)-key))-1].upper())
  30.                 else:
  31.                     ectd_li.append(alphabet[(len(alphabet)-(len(alphabet)-key))-1])
  32.                    
  33.                
  34.  
  35.     print "Encrypted text: ",
  36.     for char in ectd_li:
  37.         print char,
  38.  
  39.    
  40. def decrypt(li, key):
  41.    
  42.         for x in ectd_li:
  43.             if x == "":
  44.                 decr_li.append("")
  45.             for a in alphabet:
  46.                 if x.lower() == a.lower():
  47.                     if x == x.upper():
  48.                         decr_li.append(alphabet[alphabet.index(a)-key].upper())
  49.                     else:
  50.                         decr_li.append(alphabet[alphabet.index(a)-key])
  51.  
  52.         print "Decrypted text: ",
  53.         for char in decr_li:
  54.             print char,
  55.  
  56.  
  57. encrypt(raw_input("Text you want encrypted: "), input("Encryption key: "))
  58. print
  59. decrypt(ectd_li,input("Enter the right key to decrypt the message: "))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement