Vigenere Cipher outputting only on character

145 views Asked by At

I'm having issues getting my output to shift the amount asked in my program. It works when my decoded word is one letter and my keyword is only one letter. Any solutions to fix the output so that it can shift multiple letters rather than just one?

 def functions(decoded_list, keyword_list, ):
    shift_keyword_list = []
    position_decoded_list = []

    for letters in keyword_list:
        if letters in alpha:
            shift_keyword_list.append(alpha.index(letters))

    for letters in decoded_list:
        if letters in alpha:
            position_decoded_list.append(alpha.index(letters))

    return(shift_keyword_list, position_decoded_list)

alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'


decoded = input("Enter an english phrase in lowercase: ")
decoded_list = list(decoded.upper())

keyword = input("Enter your key: ")
keyword_list = list(keyword.upper())

# v1 = keyword_list // v2 = decoded_list
v1, v2 = functions(decoded_list, keyword_list)  

# changes the lists into integers
# int_v1 = decoded_list // int_v2 = keyword_list

int_v1 = (v1)
string_v1 = [str(int_v1) for int_v1 in int_v1]
str_v1 = ''.join(string_v1)
int_v1 = int(str_v1)

int_v2 = v2
string_v2 = [str(int_v2) for int_v2 in int_v2]
str_v2 = ''.join(string_v2)
int_v2 = int(str_v2)

encoded = ''

for letters in range(int_v1):
    encoded += alpha[(int_v2 + int_v1) % 26]
    print(encoded.upper())
    break



    
1

There are 1 answers

1
Tim Roberts On

Since the purpose of this is to encode text, you want your final loop to loop through the input text, not just a simple range. Something like this:

for letters in decoded_list:
    encoded += alpha[(ord(letters)+int_v2 + int_v1) % 26]
print(encoded.upper())

However, what you're doing is not a Vigenere cipher, which is much simpler than what you've done. Here is a Vigenere cipher:

decoded = input("Enter an english phrase: ")
keyword = input("Enter your key: ")

# Convert to ordinals.

plain = [ord(k) for k in decoded]
keywd = [ord(k) for k in keyword]

# Expand the keyword to match the length of the plaintext.

keywd = keywd * (len(plain)//len(keywd) + 1)

# Do the shift and convert back to text.

A = ord('A')
cipher = ''.join( [chr(A + ((p+k)%26)) for p,k in zip(plain,keywd)] )
print(cipher)