Make Vigenere Decipher case-insensitive in Python

118 views Asked by At

I am currently working on a school project where I need to build a decrypter for the Vigenere Cipher. The deciphering is working out just fine, but I have a problem which is that every letter is lowercase. I know the reason for this is that my used the casefold() method for my initial string. I did it so I can compare it to the alphabet a-z. Now here is my question: How can I modify the code in so that it will return the corresponding letter in uppercase?

def decipher(key):    
        original = "Rpoe, Xknf cne Rosvuhcl"
        string = original
        Alphabet ="abcdefghijklmnopqrstuvwxyz"
        dec_message = ""
        key = key.lower()
        string = string.casefold()
        textlength = len(string)
        exp_key = key
        exp_key_length = len(exp_key)
        while exp_key_length < textlength:
            exp_key = exp_key + key
            exp_key_length = len(exp_key)
        key_position = 0
        for letter in string:
            if letter in Alphabet:
                position = Alphabet.find(letter)
                key_char = exp_key[key_position]
                key_char_position = Alphabet.find(key_char)
                key_position += 1 
                new_position = position - key_char_position
                if new_position > 26:
                    new_position = new_position + 26
                new_character = Alphabet[new_position]
                dec_message = dec_message + new_character
            else:
                dec_message = dec_message + letter
        print(dec_message)
        print(original)
        return(dec_message)
            
decipher("ABC")

I tried to use a for loop so I can look at each letter in the encrypted string and then .upper the letter at the corresponding position in the dec_message string, however, that didn´t work. The encrypted text is: Rome, Wine and Portugal

0

There are 0 answers