4

The Vigènere encryption output includes an original message

 2 years ago
source link: https://www.codesd.com/item/the-vigenere-encryption-output-includes-an-original-message.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The Vigènere encryption output includes an original message

advertisements

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.


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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK