Search code examples
pythonencryptionvigenere

How to add another key into a vigenere cipher


I am writing a vigenere cipher and would like to add another key. this with further encrypt my text entered. Can anyone explain how to do this?

LETTERS = 'ZABCDEFGHIJKLMNOPQRSTUVWXY'
def main():
    myMode = input("Encrypt 'e' or Decrypt 'd': ")
    myMessage = input('What text would you like to encrypt: ')
    myKey = input('Enter your key: ')
    myKey2 = input('Enter your second key: ')
    if myMode == 'encrypt' or myMode == 'e':
        translated = encryptMessage(myKey, myMessage)
    elif myMode == 'decrypt' or myMode == 'd':
        translated = decryptMessage(myKey, myMessage)
    print('%sYour 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 = []

    keyIndex = 0
    key = key.upper())

    for symbol in message: 
        num =LETTERS .find(symbol.upper())
        if num != -1: 
            if mode == 'encrypt':
                num += LETTERS .find(key[keyIndex]) 
            elif mode == 'decrypt':
                num -= LETTERS .find(key[keyIndex])

            num %= len(LETTERS)

            if symbol.isupper():
                translated.append(LETTERS[num])
            elif symbol.islower():
                translated.append(LETTERS[num].lower())

            keyIndex += 1 
            if keyIndex == len(key):
                keyIndex = 0
        else:
            translated.append(symbol)
    return ''.join(translated)
if __name__ == '__main__':
    main()

I have already created the variable but unsure where to use them. Any help would be great.


Solution

  • At the beginning of translateMessage() you can change key to keys. Then check to see if keys is a string. (if isinstance(keys, str)) If it is, say keys = [keys] to make it a list. Then, put the rest of your function in a for loop like this: for key in keys: