解码Ceasar密码最终出现逻辑错误

发布于 2025-01-25 07:33:22 字数 1816 浏览 3 评论 0原文

在编码方面,我是一个完整的初学者,当我遇到这个问题时,我正在在密码期进行培训:

一个国家的士兵使用Ceasar Cipher进行消息传递。只有字母更改,并且在这些消息中没有触摸其他所有内容(例如“!”和空间)。每个消息分为4或5个部分,每个部分由一个跑步者携带。第一部分以2个字母开头:第一个字母显示一个随机字母,第二部分是通过换档更改的同一字母(以便士兵知道班次的数量)。现在编写一个用于解码和编码消息的代码我应该知道您将为我提供一个完美的答案!!!

编码函数有效。但是,我为解码编写的以下功能没有。我输入“ ijj tipvme ibw”,“ f lopxo uibu z”,“ pv xpvme ibwf”,“ b qfsgfdu botx”,“ fs gps nf!”!获取我应该知道您将为我提供一个完美的答案!!,但是我收到一些胡说八道的消息,说'Ž〜〜™ - '',',' Š££© - ©®','“”€ª€™ - «Š',' !!!

i导入re首先:

import re

这是功能:

def decode(arr):
    arr = str(arr) #turn what we have into str
    array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
                                # it will be a single whole messege in the end and also turn it into
                                #a list in which every item is a single part

    decoded_messege = ""

    first_messege = list(array[0]) #turns the first part into a list of letters
    letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift

    for messege in array:
        for letter in messege:
            if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
                decoded_messege += letter

            else: #the letter is an alphabet
                decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number
                                                                #minus the shift

    decoded_message = decoded_messege[2:] #ignore the first two letters that are
                                        #only for showing shift and don't matter in the message
    return decoded_messege

I'm a complete beginner when it comes to coding, I was training in CodeWars when I came across this question:

The soldiers of a country use ceasar cipher for messaging. Only letters are changed and everything else is not touched in these messages (like "!" and space). Each message is split into 4 or 5 parts and each part is carried by one runner. The first part starts with 2 letters: The first one shows a random letter and the second one is the same letter that has been changed by shifting (so that the soldiers know the number of the shift). Now write a code for decoding and encoding the message I should have known that you would have a perfect answer for me!!!

The encoding function works. However, the following function that I wrote for decoding does not. I enter the "ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!" message and I'm supposed to get I should have known that you would have a perfect answer for me!!!, but instead I get some nonsense message saying 'ž~ ¨¤ª¡™ –«', ' "š £¤¬£ ©–© ®', ' "¤ª ¬¤ª¡™ –«š ', ' "– ¥š§›š˜© –£¨¬', ' "š§ ›¤§ ¢š!!!

I import re first:

import re

Here's the function:

def decode(arr):
    arr = str(arr) #turn what we have into str
    array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
                                # it will be a single whole messege in the end and also turn it into
                                #a list in which every item is a single part

    decoded_messege = ""

    first_messege = list(array[0]) #turns the first part into a list of letters
    letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift

    for messege in array:
        for letter in messege:
            if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
                decoded_messege += letter

            else: #the letter is an alphabet
                decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number
                                                                #minus the shift

    decoded_message = decoded_messege[2:] #ignore the first two letters that are
                                        #only for showing shift and don't matter in the message
    return decoded_messege

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2025-02-01 07:33:22

问题在于您要考虑到前两个字符来计算偏移: - &gt; 是0。将其更改为arr = str(arr)[2:]
另外,返回应处于第一个循环的相同级别。

import re


def decode(arr):
    arr = str(arr)[2:] #turn what we have into str
    array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
                                    # it will be a single whole messege in the end and also turn it into
                                    #a list in which every item is a single part

    decoded_messege = ""

    first_messege = list(array[0]) #turns the first part into a list of letters
    letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift

    for messege in array:
        for letter in messege:
            if letter != '"':
                # print(letter)
                if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
                    decoded_messege += letter
                else: #the letter is an alphabet
                    decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number                                                                    #minus the shift
        decoded_message = decoded_messege[2:] #ignore the first two letters that are
                                            #only for showing shift and don't matter in the message
    return decoded_messege

print(decode('""ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!""'))
# hiI should hav e known that y ou would have  a perfect answ er for me!!!
```

The problem is that you're taking into account the first two characters to calculate the shift: "-->", which is 0. Change it to arr = str(arr)[2:]
Also, the return should be at the same level of the first loop.

import re


def decode(arr):
    arr = str(arr)[2:] #turn what we have into str
    array = list(re.split('",', arr)) #get rid of the ' ", ' between every part so that
                                    # it will be a single whole messege in the end and also turn it into
                                    #a list in which every item is a single part

    decoded_messege = ""

    first_messege = list(array[0]) #turns the first part into a list of letters
    letter_shift = ord(first_messege[1]) - ord(first_messege[0]) #realizes the number of shift

    for messege in array:
        for letter in messege:
            if letter != '"':
                # print(letter)
                if (ord(letter) > 122 or ord(letter) < 65): #the letter is not an alphabet (a space or !)
                    decoded_messege += letter
                else: #the letter is an alphabet
                    decoded_messege += chr(ord(letter) - letter_shift) #the ascii code is the current ascii number                                                                    #minus the shift
        decoded_message = decoded_messege[2:] #ignore the first two letters that are
                                            #only for showing shift and don't matter in the message
    return decoded_messege

print(decode('""ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!""'))
# hiI should hav e known that y ou would have  a perfect answ er for me!!!
```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文