#Activity 14.1.13 - Vignenere cipher (with comments)

def vigenere_cipher(message, keyword):
# A list of the characters that can be encrypted.
    characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
                'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

    keyList = []
    #An array to hold the keyword list that has the same number of characters as the message to be encrypted

    keyLength = 0
    while keyLength < len(message):
        #Makes the key the same length as the message to be encrypted
        for char in keyword:
            if keyLength < len(message):
                keyList.append(char)
                keyLength += 1
                
    #Each processed character is appended to the array encryptedChar
    encryptedChars = [] 

    encryptedCharIndexValue = 0
    keyIncrement = 0
    #Iterates through the message to be encrypted
    for char in message:
        if char not in characters:
            encryptedChars.append(char)
        else:
            #Adds together the index value of the keyword character and the message character
            encryptedCharIndexValue = characters.index(keyList[keyIncrement]) + characters.index(char)

            while encryptedCharIndexValue > 25:
                encryptedCharIndexValue = encryptedCharIndexValue - 26 

            #Appends the encrypted character to the array encryptedChars
            encryptedChars.append(characters[encryptedCharIndexValue])
       
        keyIncrement += 1

    return ''.join(encryptedChars)
    #Returns a string by joining together all the individual letters stored in the array encryptedChars

### main program ###
messageToEncrypt = input('Message: ')
keyword = input('Enter keyword: ')
encryptedMessage = vigenere_cipher(messageToEncrypt, keyword)
print(encryptedMessage)
