Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Sample Encryption Program for beginners
- # This program only handles lowercase alphabetical letters.
- import time
- def encrypt(phrase):
- ''' (string) -> string
- Return an encrypted version of the inputted phrase
- through scrambling along with other substitutions.
- >>> encrypt("fries")
- "51FRe"
- >>> encrypt("personality")
- "Y7a0RPe5N1T"
- '''
- encryption_dict = {'l':1,'z':2,'s':5,'b':6,'i':7,'g':9,'o':0, ' ':8}
- encrypted = ''
- scrambled = scramble(phrase)
- for i in scrambled:
- if i in encryption_dict:
- encrypted += str(encryption_dict.get(i))
- elif i in 'aeiou':
- encrypted += i
- else:
- encrypted += i.upper()
- return encrypted
- def scramble(phrase):
- ''' (string) -> string
- Return a scrambled version of the inputted phrase
- through multiple reverses.
- >>> scramble("fries")
- "sifre"
- >>> scramble("personality")
- "yiaorpesnlt"
- '''
- scrambled = ''
- phrase_half = len(phrase) // 2
- scrambled = reverse(phrase[0::2]) + phrase[1::2]
- return scrambled
- def reverse(phrase):
- ''' (string) -> string
- Return a reversed version of the inputted phrase.
- >>> reverse("kill")
- "llik"
- >>> reverse("drag")
- "gard"
- '''
- reversal = ''
- for i in phrase:
- reversal = i + reversal
- return reversal
- def check(phrase):
- ''' (string) -> bool
- Return True if the inputted string contains only
- alphabetical characters or whitespace and false otherwise.
- '''
- for i in phrase:
- if not(i.isalpha() or i == " "):
- return False
- return True
- # Main function
- print("Welcome to my encryption program!")
- print("This program only handles lowercase alphabetical letters.")
- time.sleep(0.5)
- more = True
- while more:
- clause = input("\nPlease enter a phrase: ")
- while not check(clause):
- print("An invalid phrase has been entered.")
- clause = input("\nPlease enter a valid phrase: ")
- if not clause.islower():
- print("Converting phrase to lowercase ...")
- time.sleep(1)
- print("Encrypting your message ...")
- time.sleep(2)
- print(reverse(clause.lower()))
- print(scramble(clause.lower()))
- print(encrypt(clause.lower()))
- answer = input("\nWould you like to encrypt another phrase? (Y/N)")
- while not(answer.lower() == 'n' or answer.lower() == 'y'):
- print("Invalid input.")
- answer = input("\nWould you like to encrypt another phrase? (Y/N)")
- if answer.lower() == 'n':
- more = False
- elif answer.lower() == 'y':
- more = True
- time.sleep(1)
- print("All done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement