Associate
- Joined
- 20 Apr 2015
- Posts
- 1
Hi,
For Computing GCSE coursework we have to code in Python a Vigenere cipher i have started to do this using code i have sourced from different places on the internet. However i am struggling to make the program work and to make it not bring up any errors.
Any help or tips would be greatly appreciated.
For Computing GCSE coursework we have to code in Python a Vigenere cipher i have started to do this using code i have sourced from different places on the internet. However i am struggling to make the program work and to make it not bring up any errors.
Any help or tips would be greatly appreciated.
#This is my code
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def getMessage():
print('Enter your message:')
return input()
def getKey():
while True:
print('Enter the key number')
return input()
def getMode():
while True:
print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()
if mode in 'encrypt decrypt'.split():
return mode
else:
print('Enter either "encrypt" or "decrypt".')
def main():
# This text can be copy/pasted from http://invpy.com/vigenereCipher.py
myMessage = getMessage()
myKey = getKey()
myMode = getMode()
if myMode == 'encrypt':
translated = encryptMessage(myKey, myMessage)
elif myMode == 'decrypt':
translated = decryptMessage(myKey, myMessage)
print('%sed message:' % (myMode.title()))
print(translated)
print()
def encryptMessage(key, message):
return translateMessage(key, message, 'encrypt')
def decryptMessage(key, message):
return translateMessage(key, message, 'decrypt')
def translateMessage(key, message, mode):
translated = [] # stores the encrypted/decrypted message string
keyIndex = 0
key = key.upper()
for symbol in message: # loop through each character in message
num = LETTERS.find(symbol.upper())
if num != -1: # -1 means symbol.upper() was not found in LETTERS
if mode == 'encrypt':
num += LETTERS.find(key[keyIndex]) # add if encrypting
elif mode == 'decrypt':
num -= LETTERS.find(key[keyIndex]) # subtract if decrypting
num %= len(LETTERS) # handle the potential wrap-around
# add the encrypted/decrypted symbol to the end of translated.
if symbol.isupper():
translated.append(LETTERS[num])
elif symbol.islower():
translated.append(LETTERS[num].lower())
keyIndex += 1 # move to the next letter in the key
if keyIndex == len(key):
keyIndex = 0
else:
# The symbol was not in LETTERS, so add it to translated as is.
translated.append(symbol)
return ''.join(translated)
# If vigenereCipher.py is run (instead of imported as a module) call
# the main() function.
if __name__ == '__main__':
main()
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def getMessage():
print('Enter your message:')
return input()
def getKey():
while True:
print('Enter the key number')
return input()
def getMode():
while True:
print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()
if mode in 'encrypt decrypt'.split():
return mode
else:
print('Enter either "encrypt" or "decrypt".')
def main():
# This text can be copy/pasted from http://invpy.com/vigenereCipher.py
myMessage = getMessage()
myKey = getKey()
myMode = getMode()
if myMode == 'encrypt':
translated = encryptMessage(myKey, myMessage)
elif myMode == 'decrypt':
translated = decryptMessage(myKey, myMessage)
print('%sed message:' % (myMode.title()))
print(translated)
print()
def encryptMessage(key, message):
return translateMessage(key, message, 'encrypt')
def decryptMessage(key, message):
return translateMessage(key, message, 'decrypt')
def translateMessage(key, message, mode):
translated = [] # stores the encrypted/decrypted message string
keyIndex = 0
key = key.upper()
for symbol in message: # loop through each character in message
num = LETTERS.find(symbol.upper())
if num != -1: # -1 means symbol.upper() was not found in LETTERS
if mode == 'encrypt':
num += LETTERS.find(key[keyIndex]) # add if encrypting
elif mode == 'decrypt':
num -= LETTERS.find(key[keyIndex]) # subtract if decrypting
num %= len(LETTERS) # handle the potential wrap-around
# add the encrypted/decrypted symbol to the end of translated.
if symbol.isupper():
translated.append(LETTERS[num])
elif symbol.islower():
translated.append(LETTERS[num].lower())
keyIndex += 1 # move to the next letter in the key
if keyIndex == len(key):
keyIndex = 0
else:
# The symbol was not in LETTERS, so add it to translated as is.
translated.append(symbol)
return ''.join(translated)
# If vigenereCipher.py is run (instead of imported as a module) call
# the main() function.
if __name__ == '__main__':
main()