Advertisement
Guest User

Sample Encryption Program for Beginners

a guest
Dec 16th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. # Sample Encryption Program for beginners
  2. # This program only handles lowercase alphabetical letters.
  3. import time
  4.  
  5. def encrypt(phrase):
  6.     ''' (string) -> string
  7.     Return an encrypted version of the inputted phrase
  8.     through scrambling along with other substitutions.
  9.     >>> encrypt("fries")
  10.     "51FRe"
  11.     >>> encrypt("personality")
  12.     "Y7a0RPe5N1T"
  13.     '''
  14.     encryption_dict = {'l':1,'z':2,'s':5,'b':6,'i':7,'g':9,'o':0, ' ':8}
  15.     encrypted = ''
  16.     scrambled = scramble(phrase)
  17.  
  18.     for i in scrambled:
  19.         if i in encryption_dict:
  20.             encrypted += str(encryption_dict.get(i))
  21.         elif i in 'aeiou':
  22.             encrypted += i
  23.         else:
  24.             encrypted += i.upper()
  25.  
  26.     return encrypted
  27.  
  28.  
  29. def scramble(phrase):
  30.     ''' (string) -> string
  31.     Return a scrambled version of the inputted phrase
  32.     through multiple reverses.
  33.     >>> scramble("fries")
  34.     "sifre"
  35.     >>> scramble("personality")
  36.     "yiaorpesnlt"
  37.     '''
  38.     scrambled = ''
  39.     phrase_half = len(phrase) // 2
  40.     scrambled = reverse(phrase[0::2]) + phrase[1::2]
  41.     return scrambled
  42.  
  43. def reverse(phrase):
  44.     ''' (string) -> string
  45.     Return a reversed version of the inputted phrase.
  46.     >>> reverse("kill")
  47.     "llik"
  48.     >>> reverse("drag")
  49.     "gard"
  50.     '''
  51.     reversal = ''
  52.     for i in phrase:
  53.         reversal = i + reversal
  54.     return reversal
  55.  
  56. def check(phrase):
  57.     ''' (string) -> bool
  58.     Return True if the inputted string contains only
  59.     alphabetical characters or whitespace and false otherwise.
  60.     '''
  61.     for i in phrase:
  62.         if not(i.isalpha() or i == " "):
  63.             return False
  64.     return True
  65.  
  66. # Main function
  67. print("Welcome to my encryption program!")
  68. print("This program only handles lowercase alphabetical letters.")
  69. time.sleep(0.5)
  70. more = True
  71. while more:
  72.     clause = input("\nPlease enter a phrase: ")
  73.  
  74.     while not check(clause):
  75.         print("An invalid phrase has been entered.")
  76.         clause = input("\nPlease enter a valid phrase: ")
  77.  
  78.     if not clause.islower():
  79.         print("Converting phrase to lowercase ...")
  80.         time.sleep(1)
  81.  
  82.     print("Encrypting your message ...")
  83.     time.sleep(2)
  84.     print(reverse(clause.lower()))
  85.     print(scramble(clause.lower()))
  86.     print(encrypt(clause.lower()))
  87.     answer = input("\nWould you like to encrypt another phrase? (Y/N)")
  88.     while not(answer.lower() == 'n' or answer.lower() == 'y'):
  89.         print("Invalid input.")
  90.         answer = input("\nWould you like to encrypt another phrase? (Y/N)")
  91.     if answer.lower() == 'n':
  92.         more = False
  93.     elif answer.lower() == 'y':
  94.         more = True
  95.     time.sleep(1)
  96.  
  97. print("All done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement