Python Forum

Full Version: coding a decoder...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, sorry to bother you, but I'm stuck on a project and I can't figure out what's wrong.
I'm coding a python text decoder (nothing very tedious, this is Caesar code, so a shift in the alphabet, with a key, which indicates how many letters you shift the text by.
I managed to write a code that, for a given text and key, will decrypt the message.
I would now like the program to test all possible keys (numbers between 1 and 25), and display each transcript associated with the key that made it possible to obtain it.
In the idea, my code is functional, but all the combinations are displayed each time, adding one after the other.
Here are the two codes, the functional one allowing to decrypt if you have the key, and the attempt that I can't put in place.

list = ['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']
for x in range(len(list)):    #double the list
    list.append(list[x])
 
message = 'RTSXNJZW'
key = int(5)
 
def dechiffragedecoding_letter(letter,list,key):
    for i in range(len(list)):
        if letter==' ':       #in case there is a space
            return ' '
        elif list[i]==letter:          
                return str(list[i-key])            
message_decoded = str()
for letter in message:
    message_decoded += decoding_letter(letter,list,key)
print(message_decoded)
list = ['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']
for x in range(len(list)):    #double the list
    liste.append(list[x])
 
message = 'RTSXNJZW'
 
def decoding_letter(letter,list,key):
   
    for i in range(len(list)):
        if letter==' ':      
            return ' '
        elif list[i]==letter:
            return str(list[i-key])
 
message_decoded = str()
 
key = 0
 
for n in range(25):
    key = key + 1    
    for letter in message:
        message_decoded += decoding_letter(letter,list,key)    
    print(key)
    print(message_decoded)
Thank you in advance for your help !

PS: for simplicity's sake, the word to decode is always the same: RTSXNJZW, which means "MONSIEUR" in capital letters, when decoded with a key = 5.
You might want to look at the thread were I posted on hacking(decrypting), the Caesar cypher)here.

If you are trying to get the decryption method to display each letter as it is found then I would think you would need to have it display the frequency of the letter(s) chosen and varied... then displayed.
Hi, thank you for your answer !
I will definitely have a look at the post you mentionned. But I just wanted to ad a little correction : I don't want to have the decryption letter by letter, but word by word, for instance :

key = 0
RTSXNJZW

key = 1
QSRWMIYV
...

key = 5
MONSIEUR

key = 6
LNMRHDTQ

But thank you anywai for your quickness, I do appreciate !