Vigenere cipher does not work

So, my teacher created this vigenere cipher, and he said that it works. However, after checking its results with vigenere online ciphers, it does not seem to lead to the correct encryption.

I have no idea how to fix this, and I was wondering if anyone could direct me to the errors and tell me how to fix them.

Here is the code

base = ord("a")
alphabets = 'abcdefghijklmnopqrstuvwxyz'  
keyword = input('What is your keyword')
message = input('What is your message to be coded or encoded?').lower()

expandedKeyword = ""
while len(expandedKeyword) < len(message): 
    for i in keyword:
        if len(expandedKeyword) < len(message): 
            expandedKeyword += i 


cipheredMessage = '' 
indexofKeyword = 0
for i in message:
        if i == ' ':
            cipheredMessage = cipheredMessage + " "
        else:
            shiftedIndex = (ord(i) + ord(expandedKeyword[indexofKeyword])-base) % 26 +base
            cipheredMessage = cipheredMessage + chr(shiftedIndex)
            indexofKeyword = indexofKeyword + 1



print(cipheredMessage)

I understand the concept of what is happening, but I can not understand the error.

+2
source share
1 answer

Your calculation is shiftedIndexincorrect, you need to subtract twice base, but you are currently subtracting it only once. Example -

shiftedIndex = (ord(i) + ord(expandedKeyword[indexofKeyword])-2*base) % 26 +base

, base ord(i), ( 'a'), ord(expandedKeyword[indexofKeyword]), ( 'a'). ( ) -

shiftedIndex = ((ord(i) - base) + (ord(expandedKeyword[indexofKeyword])-base)) % 26 + base
+1

Source: https://habr.com/ru/post/1616596/


All Articles