The Vigenère Cipher - Error in example

There is an error in the example (task 5 of Off-Platform Project: Coded Correspondence). I do not know why Codecademy do not fix this issue. I think they do it on purpose.

resulting place value: 4, 14, 23, 20, 12 … NOT 24, 12, 11, 14, 10 …

Error

An included link to the exact exercise would be appreciated by us viewers. Thanks.

1 Like

I’m not assigned anymore, so I can’t provide a link. But here is what the Vigenère Cipher should do (Spoiler: there is no issue to fix, 24, 12, 11, 14, 10 is correct).

Starting with the letters on index 0, b and d

Given the latin alphabet has a length of 26:

alphabet = []
for i in range(ord('a'), ord('z') + 1):
  alphabet.append(chr(i))

print(alphabet, len(alphabet)) # ['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'] 26

b has the index of 1 and d has the index of 3
b should be shifted within the alphabet the amount of letters resulting from the index of b minus the index of d, which is -2:

shift = (alphabet.index('b') - alphabet.index('d'))
print(shift) # -2
print(alphabet.index(alphabet[shift % len(alphabet)])) # 24
3 Likes

Hi, that makes more sense. But why
shift = (alphabet.index(‘b’) - alphabet.index(‘d’))
instead of
shift = (alphabet.index(‘b’) + alphabet.index(‘d’))

I found this formula about Vigenere Cipher:
c = (p + k) mod 26
4 = (1 + 3) mod 26

Depends whether you want to decode or encode. When you encode as advised in the example you posted, you shift to the left. When you decode, you shift to the right.

1 Like

Is this correct?
c = encrypted word
p = letter from the message
k = key

Encryption: c = (p + k) mod 26
Decryption: p = (c - k) mode 26

The other way around:

Encryption:

c = (p - k) % 26
encode('b', 'd') == (1 - 3) % 26 == 24
chr(24 + chr('a')) == 'y'

Decryption:

p = (c + k) % 26
decode('y', 'd') == (24 + 3) % 26 == 1
chr(1 + chr('a')) == 'b'

2 Likes