Cipher:
The program asks you to input a string and whether you want to encrypt or decrypt it. If you choose encrypt, you input your string and a key and it moves X ASCII characters up. If you decrypt it, you input a string and a key and it moves X ASCII characters down. More exciting stuff to come.
MAX_KEY_SIZE=26
def getMode():
while True:
print('Do you wanna encrypt or decrypt a message? '),
mode=raw_input().lower()
if mode in 'encrypt e decrypt d'.split():
return mode
else:
print('Enter either "encrypt" or "decrypt"')
def getMessage():
print('Enter your message:')
return raw_input()
def getKey():
key=0
while True:
print('Enter the key number (1-%s)'%(MAX_KEY_SIZE))
key=int(raw_input())
if(key>=1 and key<=MAX_KEY_SIZE):
return key
def getTranslatedMessage(mode, message, key):
if mode[0]=='d':
key=-key
translated=''
for symbol in message:
if symbol.isalpha():
num=ord(symbol)
num+=key
if symbol.isupper():
if num>ord('Z'):
num-=26
elif num<ord('A'):
num+=26
elif symbol.islower():
if num>ord('z'):
num-=26
elif num<ord('a'):
num+=26
translated+=chr(num)
else:
translated+=symbol
return translated
def again():
print('Translate another message? '),
return raw_input().lower().startswith('y')
while True:
mode=getMode()
message=getMessage()
key=getKey()
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
if not again():
break
No comments:
Post a Comment