fork download
  1. #Stops the program going over the 25 (because 26 would be useless)
  2. #character limit
  3. MAX = 25
  4.  
  5. #Main menu and ensuring only the 3 options are chosen
  6. def getMode():
  7. while True:
  8. print('Main Menu: Encrypt (e), Decrypt (d), Stop (s), Block Encrypt (be).')
  9. mode = input().lower()
  10. if mode in 'encrypt e decrypt d block encrypt be'.split():
  11. return mode
  12. if mode in 'stop s'.split():
  13. exit()
  14. else:
  15. print('Please enter only "encrypt", "e", "decrypt", "d", "stop", "s" or "block encrypt", "be"')
  16.  
  17. def getMessage():
  18. print('Enter your message:')
  19. return input()
  20.  
  21. #Creating the offset factor
  22. def getKey():
  23. key = 0
  24. while True:
  25. print('Enter the offset factor (1-%s)' % (MAX))
  26. key = int(input())
  27. if (key >= 1 and key <= MAX):
  28. return key
  29.  
  30. #Decryption with the offset factor
  31. def getTranslatedMessage(mode, message, key):
  32. if mode[0] == 'd':
  33. #The key is inversed so that it simply takes away the offset factor instead
  34. #of adding it
  35. key = -key
  36. translated = ''
  37.  
  38. if mode == 'be':
  39. message = message.replace(" ","")
  40.  
  41. #The spaces are all removed for the block encryption
  42.  
  43. #Ensuring that only letters are attempted to be coded
  44. for symbol in message:
  45. if symbol.isalpha():
  46. number = ord(symbol)
  47. number += key
  48. #Ensuring the alphabet loops back over to "a" if it goes past "z"
  49. if symbol.isupper():
  50. if number > ord('Z'):
  51. number -= 26
  52. elif number < ord('A'):
  53. number += 26
  54. elif symbol.islower():
  55. if number > ord('z'):
  56. number -= 26
  57. elif number < ord('a'):
  58. number += 26
  59.  
  60. translated += chr(number)
  61. else:
  62. translated += symbol
  63. return translated
  64. #Returns translated text
  65.  
  66. mode = getMode()
  67. message = getMessage()
  68. key = getKey()
  69. #Retrieving the mode, message and key
  70.  
  71. print('The translated message is:')
  72. print(getTranslatedMessage(mode, message, key))
  73. #Tells the user what the message is
Success #stdin #stdout 0.02s 8688KB
stdin
be
ab cd ef
2
stdout
Main Menu: Encrypt (e), Decrypt (d), Stop (s), Block Encrypt (be).
Enter your message:
Enter the offset factor (1-25)
The translated message is:
cdefgh