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

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

if Mode == "encrypt":
    Message=input('Please eneter the message you wish to encrypt: ')
    Keyword=input('Please enter the keyword you want to use:')
    KeywordTwo = input(' Please enter second keyword you want to use:') 
    Keywordlength = len(Keyword)
    KeywordTwolength = len(KeywordTwo)

    for i in range(0, len(Message)):
        Messageencrypt = Message[i]
        Keywordencrypt = Keyword[Keywordcount]
        KeywordTwoencrypt= KeywordTwo[KeywordTwocount]
        if Messageencrypt.isalpha():
            if Messageencrypt.isupper():
                Messageencrypt = ord(Messageencrypt)
                Keywordencrypt = Keywordencrypt.upper()
                Keywordencrypt = ord(Keywordencrypt) - 64
                KeywordTwoencrypt = KeywordTwoencrypt.upper()
                KeywordTwoencrypt = ord(Keyword2encrypt) - 64
                FirstCodedMessage = Messageencrypt + Keywordencrypt
                FinalCodedMessage = FirstCodedMessage + Keyword2encrypt
                if FinalCodedMessage > 90:
                    FinalCodedMessage = FinalCodedMessage - 26
                Encryptedletter = chr(FinalCodedMessage)
            elif Messageencrypt.islower():
                Messageencrypt = ord(Messageencrypt)
                Keywordencrypt = Keywordencrypt.upper()
                Keywordencrypt = ord(Keywordencrypt) - 64
                KeywordTwoencrypt = KeywordTwoencrypt.upper()
                KeywordTwoencrypt = ord(KeywordTwoencrypt) - 64
                FinalCodedMessage = Messageencrypt + Keywordencrypt
                if FinalCodedMessage > 122:
                    FinalCodedMessage = FinalCodedMessage - 26
            Encryptedletter = chr(FinalCodedMessage)
        else:
            Encryptedletter = Messageencrypt
        EncryptedMessage = EncryptedMessage + Encryptedletter
        Keywordcount = Keywordcount + 1
        if Keywordcount >= Keywordlength:
            Keywordcount = 0
        KeywordTwocount = KeywordTwocount + 1
        if KeywordTwocount >= Keywordlength:
            KeywordTwocount = 0
    print(EncryptedMessage)

What does

KeywordTwoencrypt= KeywordTwo[KeywordTwocount]
IndexError: string index out of range

mean? Error

Also how would I save this encrypted message as a txt file? Error

Artjom B.
  • 61,146
  • 24
  • 125
  • 222

2 Answers2

1

if KeywordTwocount >= Keywordlength: should be if KeywordTwocount >= KeywordTwolength:

If there is an index problem, then you should look closer to all parts of the code that deal with this specific index incrementation/decrementation.

In this specific case, you should see that the index of the second keyword shouldn't have anything to do with the length of the first keyword. So the updating is buggy:

if KeywordTwocount >= Keywordlength:
    KeywordTwocount = 0

This error would have been easily avoidable by defining a function that encrypts a message with one keyword:

def vigenere(message, keyword):
    ...

And then you can chain those two together:

print(vigenere(vigenere(message, keyword1), keyword2))

Writing to a file is also easy with the built-in functions:

with open('ciphertext.txt', 'w') as ct:
    ct.write(EncryptedMessage)
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
0

KeywordTwo[KeywordTwoCount] IndexError: string index out of range means that the index (KeywordTwoCount) is greater than the length of the string you're indexing in to KeywordTwo.

poolie
  • 9,289
  • 1
  • 47
  • 74