Vigener cipher-the script outputs an incorrect result

The problem is that the code works, but gives the wrong result.

Example for the Vigener cipher:

If we take the word "CODE" as the key and encrypt the word "TABLET", then you need to shift each letter as follows:
● "T" offset "C" (shift by 2 positions) and replaced with "V"
● "A" is shifted to "O" (slide 14 positions) and replaced by "O"
● "B" is shifted to "D" (shift by 3 positions) and replaced by "E"
● "L" offset "E" (shift by 4 positions) and replaced by "P"
● "E" offset "C" (shift by 2 positions) and replaced by "G"
● "T" offset "O" shift (14 positions) and replaced by "H"

alphabet = 'abcdefghijklmnopqrstuvwxyz'
s = input("слово для шифрования(латиница) - ")
key = input("ключ - ")
res,j = '',0
for i in range(len(s)):
   res += alphabet[(alphabet.index(s[i]) + alphabet.index(key[j])) % len(s)]
   if j == len(key)-1: j = 0
   else: j += 1
print(res)

Conclusion:

Слово для шифрования(латиница) - java
    ключ - code
    dcaa

The expected result is loye, not dcaa.

Author: 0xdb, 2020-04-02

1 answers

You are not working with the modules quite correctly. And it should be cut to the length of the alphabet, not the length of the word.

for x in s:
   res += alphabet[(alphabet.index(x) + alphabet.index(key[j])) % (len(alphabet)-1)]
   j = (j+1) % (len(key)-1)
 5
Author: splash58, 2020-04-02 22:19:40