Coded Correspondence - Step 5 Question

I’ve been working on the Coded Correspondence Off-Platform Project and I am running into issues with the fifth challenge. I have coded the following solution to the Vigenere Decoder:

alphabet = 'abcdefghijklmnopqrstuvwxyz'
encoded_message = 'txm srom vkda gl lzlgzr qpdb? fepb ejac! ubr imn tapludwy mhfbz cza ruxzal wg zztcgcexxch!'
decoded_message = ''
keyword = 'friends'
keyword_phrase = ''
keyword_index = 0

for character in encoded_message:
    if keyword_index >= len(keyword):
        keyword_index = 0
    if character in alphabet:
        keyword_phrase += keyword[keyword_index]
        keyword_index += 1
    else:
        keyword_phrase += character

for index in range(len(encoded_message)):
    index_of_message = alphabet.find(encoded_message[index])
    index_of_keyword = alphabet.find(keyword_phrase[index])
    if index_of_message == -1:
        decoded_message += encoded_message[index]
    else:
        decoded_message += alphabet[index_of_message - index_of_keyword]

print(decoded_message)

The outcome the I receive is ‘oge oelu qtvw ti tuuyve nxyk? xacy meju! qoo qhw lwcicyfq iucju lrw erfujd st wholyyrufxq!’ This matches up with every Vigenere solver I am able to find online, however, it appears that the answer I was supposed to receive was ‘you were able to decode this? nice work! you are becoming quite the expert at crytography!’ If I change the ‘keyword’ to ‘vjswnxi’, I do receive that output. Am I misunderstanding the logic of the Vigenere cipher?

You are creating the keyword_phrase correctly. But then you are decoding in the wrong direction:

It must not be the index of the message MINUS the index of the keyword (that’s for encoding), but the index of the message PLUS the index of the keyword.
When you arrive at the end of the alphabet, you continue at the beginning.

1 Like

Yeah, I re-read the challenge and agree that I had mis-interpreted the direction I needed to go. As an aside, is it possible that the instructions are incorrect? Every single Vigenere solver online outputted a string that matched my output, as opposed to the output that the challenge wanted me to get.

I see what you mean now and I guess you are right. This is what I found on Wikipedia:

string verschlüsseln(string klartext, string schlüssel)
{
    string ausgabe;
    for (int i = 0; i < klartextlänge; i++) // Schleife, die die Zeichen des Textes durchläuft
    {
        // Verschlüsselt ein Zeichen des Textes und fügt es dem Ausgabestring an
        ausgabe += (klartext[i] + schlüssel[i % schlüssellänge]) % 26 + 'A';
    }
    return ausgabe;
}

string entschlüsseln(string geheimtext, string schlüssel)
{
    string ausgabe;
    for (int i = 0; i < geheimtextlänge; i++)
    {
        ausgabe += (geheimtext[i] - schlüssel[i % schlüssellänge] + 26) % 26 + 'A';
        // 26 wird addiert für den Fall, dass die Division negativ ist
    }
    return ausgabe;
}

verschlüsseln = encode
entschlüsseln = decode

Yeah, that is what I was wondering! My bad logic actually got me to the “right” answer, so I was confused. Thanks for helping me get to the solution the system was looking for :slight_smile:

1 Like