Mr H
Mr H

Reputation: 11

Vigènere cipher output includes original message

I am quite new to python and am having some trouble getting a vigenere cipher program to work. The program will take in a message and a keyword and will allow a user to either encrypt or decrypt the message based upon the given information. I have added the code below.

LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def main():
    myMessage = input("Message:")
    myKey = input("Key:")
    myMode = input("Encrypt or decrypt:")

    if myMode == 'encrypt':
        translated = encryptMessage(myKey, myMessage)
    elif myMode == 'decrypt':
        translated = decryptMessage(myKey, myMessage)

    print('%sed message:' % (myMode.title()))
    print(translated)

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()

When I run the program it comes up with this:

Message:hello
Key:pizza
Encrypt or decrypt:encrypt
Encrypted message:
whmeklklo

When it encrypts the information it includes the letters from the original message as well as the encrypted one. I am a bit stumped as to how to fix this.

Upvotes: 1

Views: 110

Answers (2)

Cody Stevens
Cody Stevens

Reputation: 414

If I understand what you are asking, then you can see you are adding the original symbol back in to the translated with this code:

        else:
            translated.append(symbol)

Upvotes: 0

thanasis2028
thanasis2028

Reputation: 125

you are adding original character in the new string with:

else:
    translated.append(symbol)

if you remove this it should work.

Edit: also the way you are using strings is a bit awkward, here is a new version of translate that looks a bit better:

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 += LETTERS[num]
            elif symbol.islower():
                translated += LETTERS[num].lower()

            keyIndex += 1
            if keyIndex == len(key):
                keyIndex = 0

    return translated

Upvotes: 1

Related Questions