I wrote an encryption/decryption algorithm for the Caesar Cipher. It works fine, but I'm curious if there are any constructive critiques, tips, tricks, comments, or advice anyone may have on how the code operates, performs, looks, etc.
# Caesar Cipher
def chooseMode():
while True:
mode = raw_input("\nChoose a mode (encryption or decryption):\n 1) Encrypt \n 2) Decrypt\n 3) Brute Force\n\n")
if mode in ['1', '2', '3']:
return mode
def collectMessage():
message = raw_input("Enter the message you would like to translate:\n\n")
return message
def collectKey():
while True:
key = raw_input("What is the key for your message? (Enter a number from 0 to 26): \n")
try:
if int(key) in range(27):
return int(key)
else:
print("Please enter an integer between 0 and 26.\n")
continue
except ValueError:
print("Please enter an integer between 0 and 26.\n")
continue
def translateMessage(text, cipher_mode, caeser_key):
translation = ''
# If decrypting, convert key to negative.
if cipher_mode == '2':
caeser_key = -caeser_key
for symbol in text:
# Convert only Alphabetical Symbols
if symbol.isalpha():
num = ord(symbol)
# Shift num down to 1 through 26 for more easier to visualize modulous.
# Then Apply the Key; then Modulate; then Move Back Up; then Build into Chr and Translation
if symbol.isupper():
num -= ord('A')
num += caeser_key
num %= 26
num += ord('A')
translation += chr(num)
else:
num -= ord('a')
num -= caeser_key
num %= 26
num += ord('a')
translation += chr(num)
else:
translation += symbol
return translation
def bruteForce(text, cipher_mode):
if cipher_mode == '3':
for index in range(26):
print "A Caeser Key of ", str(index), "reveals:\t", translateMessage(text, cipher_mode, 26 - index)
exit()
else:
return None
choice_of_mode = chooseMode()
entered_message = collectMessage()
bruteForce(entered_message, choice_of_mode)
entered_key = collectKey()
final_form = translateMessage(entered_message, choice_of_mode, entered_key)
print final_form