Advertisement
Adehumble

Caesar Cipher

May 4th, 2020
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. import a
  2.  
  3. coded=[]
  4. choice="y"
  5. l_alpha=list(string.ascii_lowercase)
  6. u_alpha=list(string.ascii_uppercase)
  7.    
  8. #Function to Encrypt
  9. def encryption(sentence):
  10.     for i in sentence:
  11.         if i in l_alpha:
  12.             id=l_alpha.index(i)-3
  13.             coded.append(l_alpha[id])
  14.            
  15.         elif i in u_alpha:
  16.             id=u_alpha.index(i)-3
  17.             coded.append(u_alpha[id])
  18.            
  19.         elif not i.isalpha():
  20.             coded.append(i)
  21.    
  22.     encrypted="".join(coded)
  23.    
  24.     print(encrypted)
  25.  
  26. #Function to Decrypt  
  27. def decryption(sentence):
  28.     for i in sentence:
  29.         if i in l_alpha:
  30.             id=l_alpha.index(i)
  31.             if id>22:
  32.                 id=id-23
  33.                 coded.append(l_alpha[id])
  34.             else:
  35.                 id+=3
  36.                 coded.append(l_alpha[id])
  37.            
  38.         elif i in u_alpha:
  39.             id=u_alpha.index(i)
  40.             if id>22:
  41.                 id=id-23
  42.                 coded.append(u_alpha[id])
  43.             else:
  44.                 id+=3
  45.                 coded.append(u_alpha[id])
  46.            
  47.         elif not i.isalpha():
  48.             coded.append(i)
  49.    
  50.         decrypted="".join(coded)
  51.    
  52.     print(decrypted)
  53.    
  54.  
  55. while (choice != "Q" or choice !="q"):    
  56.     choice=input("What would you like to do?\nPress 'A': To encrypt a message\nPress 'B': To decrypt a message\n ")
  57.    
  58.     if (choice == "A" or choice == "a"):
  59.         print()
  60.         message=input("Enter the sentence you would like to encrypt: ")
  61.         print(f"Your message has been encrpted to: ", end="")
  62.         encryption(message)
  63.         print("\nYou may press 'Q' to quit")
  64.         print()
  65.        
  66.     elif (choice=="B" or choice == "b"):
  67.         print()
  68.         message=input("Enter the sentence you would like to decrypt: ")
  69.         print(f"Your message has been decrypted. Your original message was: ", end="")
  70.         decryption(message)
  71.         print("\nYou may press 'Q' to quit")
  72.         print()
  73.        
  74.     elif (choice =="Q" or choice == 'q'):
  75.         print()
  76.         print("Thank you. Pls stay safe!")
  77.         break
  78.        
  79.     else:
  80.         print("That was a wrong choice. You can simply press 'Q' to Quit")
  81.         print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement