Coded Correspondence

Hi. After the Thread Shed posts on the forum from March 21 to April 12, I am getting stuck again with the Coded Correspondence project. It is not a problem per se, but rather a slight correction. The offsets for the messages (Caesar) were 10 and 14, but in the actual problem they were 16 and 12. I will send the code with the correct offsets of 16 and 12, and the wrong offsets of 10 and 14.

OFFSET 10:

alphabet = "abcdefghijklmnopqrstuvwxyz"
message = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!"

def caesar_decode(code, offset):
    decoded_message = ""
    for letter in code:
        if not(letter in alphabet):
            decoded_message += letter
        else:
            letter_position = alphabet.find(letter)
            lp_new = letter_position - offset
            decoded_letter = alphabet[lp_new]
            decoded_message += decoded_letter
    return decoded_message

print(caesar_decode(message, 10))
nke znkxk! znoy oy gt kdgsvrk ul g igkygx iovnkx. ckxk eua ghrk zu jkiujk oz? o nuvk yu! yktj sk g skyygmk hgiq cozn znk ygsk ullykz!

Should be print(caesar_decode(message, 16)) not print(caesar_decode(message, 10)).

alphabet = "abcdefghijklmnopqrstuvwxyz"
message = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!"

def caesar_decode(code, offset):
    decoded_message = ""
    for letter in code:
        if not(letter in alphabet):
            decoded_message += letter
        else:
            letter_position = alphabet.find(letter)
            lp_new = letter_position - offset
            decoded_letter = alphabet[lp_new]
            decoded_message += decoded_letter
    return decoded_message

print(caesar_decode(message, 16))
hey there! this is an example of a caesar cipher. were you able to decode it? i hope so! send me a message back with the same offset!

OFFSET 14:
The first coded message in Vishal’s second letter (with offset 16, of course) points that the decode offset of the second message in that letter is 14.

alphabet = "abcdefghijklmnopqrstuvwxyz"
message = "jxu evviuj veh jxu iusedt cuiiqwu yi vekhjuud."

def caesar_decode(code, offset):
    decoded_message = ""
    for letter in code:
        if not(letter in alphabet):
            decoded_message += letter
        else:
            letter_position = alphabet.find(letter)
            lp_new = letter_position - offset
            decoded_letter = alphabet[lp_new]
            decoded_message += decoded_letter
    return decoded_message

print(caesar_decode(message, 16))
the offset for the second message is fourteen.

However, when I apply offset 14 to message 2, something fails.

alphabet = "abcdefghijklmnopqrstuvwxyz"
message = "bqdradyuzs ygxfubxq omqemd oubtqde fa oapq kagd yqeemsqe ue qhqz yadq eqogdq!"

def caesar_decode(code, offset):
    decoded_message = ""
    for letter in code:
        if not(letter in alphabet):
            decoded_message += letter
        else:
            letter_position = alphabet.find(letter)
            lp_new = letter_position - offset
            decoded_letter = alphabet[lp_new]
            decoded_message += decoded_letter
    return decoded_message

print(caesar_decode(message, 14))
ncpdmpkgle ksjrgnjc aycqyp agnfcpq rm ambc wmsp kcqqyecq gq ctcl kmpc qcaspc!

Should be print(caesar_decode(message, 12)), not print(caesar_decode(message, 14))

alphabet = "abcdefghijklmnopqrstuvwxyz"
message = "bqdradyuzs ygxfubxq omqemd oubtqde fa oapq kagd yqeemsqe ue qhqz yadq eqogdq!"

def caesar_decode(code, offset):
    decoded_message = ""
    for letter in code:
        if not(letter in alphabet):
            decoded_message += letter
        else:
            letter_position = alphabet.find(letter)
            lp_new = letter_position - offset
            decoded_letter = alphabet[lp_new]
            decoded_message += decoded_letter
    return decoded_message

print(caesar_decode(message, 12)
performing multiple caesar ciphers to code your messages is even more secure!

So was this intentional or not?

It was not intentional but I see why it’s working like this. Firstly, the definition of a Caesar Cypher from the text is:

“You take your message, something like “hello” and then you shift all of the letters by a certain offset. For example, if I chose an offset of 3 and a message of “hello”, I would code my message by shifting each letter 3 places to the left (with respect to the alphabet). So “h” becomes “e”, “e” becomes, “b”, “l” becomes “i”, and “o” becomes “l”. Then I have my coded message,“ebiil”!”

What this tells us is that when encoding a message, you shift the letters to the left by the offset i.e. you subtract the offset from the letter index. This is how you end up with ‘h’ (index 7) becoming ‘e’ (index 4) with an offset of 3.
Therefore, if we want to decode the message, you need to add back the offset that was subtracted. Thus you start with ‘e’ (index 4), add the offset of 3 and end up with ‘h’ (index 7). Thus this letter has been decoded.

In your code, you are subtracting the offset instead of adding it, and this is why the given offset does not work. however, selecting a letter from a string using [index] is cyclical, meaning that for a string of length 26, string[10] == string[-16]. This makes sense, as if you subtract 16 from 26 you get 10, therefore going 16 spaces back from the end of a string will result in the same as going 10 spaces forward from the start. So if you change it to lp_new = letter_position + offset it will work with 10 instead of 16.

However, this then creates the issue of having a letter like ‘y’ (index 24) ending up as (index 27) when adding an offset of 3, thus being out of range and getting no return. Here you then need to find a way to get the cyclical effect back, such that string[26] == string[0], string[27] == string[1] etc. See if you can figure out how to do this but I’ve left the solution below if you just want to look.

letter_position = alphabet2.find(letter)
lp_new = (letter_position + offset) % 26
3 Likes

I agree, Adam, and I just know how to do it! I use modulo division (modular Arithmetic).

script = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!" def caesar_decode(message, offset): script_num = "" alphabet = "abcdefghijklmnopqrstuvwxyz" for i in message: if i not in alphabet: script_num += i else: script_num += (alphabet[alphabet.find(i)-offset]) return script_num def caesar_encode(message, offset): script_num = "" alphabet = "abcdefghijklmnopqrstuvwxyz" for i in message: if i not in alphabet: script_num += i else: if alphabet.find(i)+offset<=25: script_num += (alphabet[alphabet.find(i)+offset]) else: script_num += alphabet[alphabet.find(i)+offset-26] return script_num print(caesar_decode(script, 16)) print("\n") first_message = "jxu evviuj veh jxu iusedt cuiiqwu yi vekhjuud." print(caesar_decode(first_message, 16)) print("\n") second_message = "bqdradyuzs ygxfubxq omqemd oubtqde fa oapq kagd yqeemsqe ue qhqz yadq eqogdq!" print(caesar_encode(second_message, 14))