Search code examples
pythonencryptionvigenere

Can someone tell me why my encrpyt works but not my decrpyt? look at picture below


#Vigenere Cipher EncryptedMessage="" Mode="" Keywordcount = 0 #sets everything to 0

while Mode != "encrypt" and Mode != "decrypt":
    Mode = input('Do you want to encrypt or decrypt a message?')
    Mode = Mode.lower()

if Mode == "encrypt":

    Keyword=input('Please enter the keyword you want to use:')

    Message=input('Please eneter the message you witsh to encrypt: ')

    Keywordlength = len(Keyword)

    for i in range(0, len(Message)):#

            Messageencrypt = Message[i]
            Keywordencrypt = Keyword[Keywordcount]

            if Messageencrypt.isalpha():

                if Messageencrypt.isupper():

                    Messageencrypt = ord(Messageencrypt)

                    Keywordencrypt = Keywordencrypt.upper()

                    Keywordencrypt = ord(Keywordencrypt) - 64

                    CodedMessage = Messageencrypt + Keywordencrypt

                    if CodedMessage > 90:
                        CodedMessage = CodedMessage - 26

                    Encryptedletter = chr(CodedMessage)          


                elif Messageencrypt.islower():

                    Messageencrypt = ord(Messageencrypt)

                    Keywordencrypt = Keywordencrypt.upper()

                    Keywordencrypt = ord(Keywordencrypt) - 64

                    CodedMessage = Messageencrypt + Keywordencrypt

                    if CodedMessage > 122:
                        CodedMessage = CodedMessage - 26


                Encryptedletter = chr(CodedMessage)

            else:
                Encryptedletter = Messageencrypt

            EncryptedMessage = EncryptedMessage + Encryptedletter

            Keywordcount = Keywordcount + 1

            if Keywordcount >= Keywordlength:
                Keywordcount = 0

    print(EncryptedMessage)


if Mode == "decrypt":

    Keyword=input('Please enter the keyword you want to use:')

    Message=input('Please eneter the message you witsh to decrypt: ')

    Keywordlength = len(Keyword)

    for i in range(0, len(Message)):#

            Messageencrypt = Message[i]
            Keywordencrypt = Keyword[Keywordcount]

            if Messageencrypt.isalpha():

                if Messageencrypt.isupper():

                    Messageencrypt = ord(Messageencrypt)

                    Keywordencrypt = Keywordencrypt.upper()

                    Keywordencrypt = ord(Keywordencrypt) - 64

                    CodedMessage = Messageencrypt - Keywordencrypt

                    if CodedMessage > 90:
                        CodedMessage = CodedMessage - 26

                    Encryptedletter = chr(CodedMessage)          


                elif Messageencrypt.islower():

                    Messageencrypt = ord(Messageencrypt)

                    Keywordencrypt = Keywordencrypt.upper()

                    Keywordencrypt = ord(Keywordencrypt) - 64

                    CodedMessage = Messageencrypt - Keywordencrypt

                    if CodedMessage > 122:
                        CodedMessage = CodedMessage - 26


                Encryptedletter = chr(CodedMessage)

            else:
                Encryptedletter = Messageencrypt

            EncryptedMessage = EncryptedMessage + Encryptedletter

            Keywordcount = Keywordcount + 1

            if Keywordcount >= Keywordlength:
                Keywordcount = 0

    print(EncryptedMessage)

the program encrypts properly but does not decrypt properly Somebody explain whats wrong

This image show what is what happens when i try to decrypt


Solution

  • Try this:

    #Vigenere Cipher
    EncryptedMessage=""
    Mode=""
    Keywordcount = 0 #sets everything to 0
    
    
    while Mode != "encrypt" and Mode != "decrypt":
        Mode = input('Do you want to encrypt or decrypt a message?')
        Mode = Mode.lower()
    
    if Mode == "encrypt":
        Keyword=input('Please enter the keyword you want to use:')
        Message=input('Please eneter the message you wish to encrypt: ')
        Keywordlength = len(Keyword)
        for i in range(0, len(Message)):
            Messageencrypt = Message[i]
            Keywordencrypt = Keyword[Keywordcount]
            if Messageencrypt.isalpha():
                if Messageencrypt.isupper():
                    Messageencrypt = ord(Messageencrypt)
                    Keywordencrypt = Keywordencrypt.upper()
                    Keywordencrypt = ord(Keywordencrypt) - 64
                    CodedMessage = Messageencrypt + Keywordencrypt
                    if CodedMessage > 90:
                        CodedMessage = CodedMessage - 26
                    Encryptedletter = chr(CodedMessage)
                elif Messageencrypt.islower():
                    Messageencrypt = ord(Messageencrypt)
                    Keywordencrypt = Keywordencrypt.upper()
                    Keywordencrypt = ord(Keywordencrypt) - 64
                    CodedMessage = Messageencrypt + Keywordencrypt
                    if CodedMessage > 122:
                        CodedMessage = CodedMessage - 26
                Encryptedletter = chr(CodedMessage)
            else:
                Encryptedletter = Messageencrypt
            EncryptedMessage = EncryptedMessage + Encryptedletter
            Keywordcount = Keywordcount + 1
            if Keywordcount >= Keywordlength:
                Keywordcount = 0
        print(EncryptedMessage)
    
    
    if Mode == "decrypt":
        Keyword=input('Please enter the keyword you want to use:')
        Message=input('Please eneter the message you wish to decrypt: ')
        Keywordlength = len(Keyword)
        for i in range(0, len(Message)):
            Messageencrypt = Message[i]
            Keywordencrypt = Keyword[Keywordcount]
            if Messageencrypt.isalpha():
                if Messageencrypt.isupper():
                    Messageencrypt = ord(Messageencrypt)
                    Keywordencrypt = Keywordencrypt.upper()
                    Keywordencrypt = ord(Keywordencrypt) - 64
                    CodedMessage = Messageencrypt - Keywordencrypt
                    if CodedMessage < 65:
                        CodedMessage = CodedMessage + 26
                    Encryptedletter = chr(CodedMessage)
                elif Messageencrypt.islower():
                    Messageencrypt = ord(Messageencrypt)
                    Keywordencrypt = Keywordencrypt.upper()
                    Keywordencrypt = ord(Keywordencrypt) - 64
                    CodedMessage = Messageencrypt - Keywordencrypt
                    if CodedMessage < 97:
                        CodedMessage = CodedMessage + 26
                Encryptedletter = chr(CodedMessage)
            else:
                Encryptedletter = Messageencrypt
            EncryptedMessage = EncryptedMessage + Encryptedletter
            Keywordcount = Keywordcount + 1
            if Keywordcount >= Keywordlength:
                Keywordcount = 0
        print(EncryptedMessage)
    

    These are the changes I made in diff format:

    --- old.py  2015-11-10 14:01:31.000000000 -0500
    +++ new.py  2015-11-10 13:56:52.000000000 -0500
    @@ -54,7 +54,7 @@
    Keywordencrypt = Keywordencrypt.upper()
    Keywordencrypt = ord(Keywordencrypt) - 64
    CodedMessage = Messageencrypt - Keywordencrypt
    -                    if CodedMessage > 90:
    +                    if CodedMessage < 65:
            CodedMessage = CodedMessage + 26
    Encryptedletter = chr(CodedMessage)
    elif Messageencrypt.islower():
    @@ -62,7 +62,7 @@
    Keywordencrypt = Keywordencrypt.upper()
    Keywordencrypt = ord(Keywordencrypt) - 64
    CodedMessage = Messageencrypt - Keywordencrypt
    -                    if CodedMessage > 122:
    +                    if CodedMessage < 97:
            CodedMessage = CodedMessage + 26
    Encryptedletter = chr(CodedMessage)
    else: