在python中实现enigma构建的插件的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在python中实现enigma构建的插件的问题相关的知识,希望对你有一定的参考价值。

在大学校园里为学校作业建造一个“喜欢”的机器,这是一项非强制性的练习。有几个指导步骤(首先我必须实现一个旋转轮,然后其中3个,在此之后我必须实现反射器,然后能够设置轮子的起始位置)。在此之前一切都很顺利(它通过了每次测试),但是当我尝试实现插板(Steckerbrett)时,我的构建无法进行测试,并且在搜索了几个小时的错误后(我找不到)我要求你帮我。

这是我的代码(请注意我仍然是noobie,我的代码可能不是最好的):

    #Enigma build (everything included)
class Enigma:
    def __init__(self, wheels, reflector, starting_position, plugs, message):
        self.message = message
        self.wheels = Wheels(wheels, starting_position)
        self.reflector = reflector
        self.plugboard = Plugboard(plugs)

        self.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        self.wheels.set_starting_position()

    def execute(self):
        output = ""

        self.message = self.plugboard.forward(self.message)

        for char in self.message:
            if char in self.alphabet:
                coded_char = char

                if coded_char in self.plugboard.plugs_forward:
                    coded_char = self.plugboard.plugs_forward[coded_char]

                coded_char = coding_forward(coded_char, self.alphabet, self.wheels.wheel1)
                coded_char = coding_forward(coded_char, self.alphabet, self.wheels.wheel2)
                coded_char = coding_forward(coded_char, self.alphabet, self.wheels.wheel3)

                coded_char = coding_forward(coded_char, self.alphabet, self.reflector)

                coded_char = coding_backward(coded_char, self.alphabet, self.wheels.wheel3)
                coded_char = coding_backward(coded_char, self.alphabet, self.wheels.wheel2)
                coded_char = coding_backward(coded_char, self.alphabet, self.wheels.wheel1)

                output = "".join([output, coded_char])
                self.wheels.rotate()

        output = self.plugboard.backward(output)

        return output

class Wheels:
    def __init__(self, wheels, position):
        self.wheel1 = wheels[0]
        self.wheel2 = wheels[1]
        self.wheel3 = wheels[2]
        self.position = position

        self.rotation_counter = 0

    def rotate(self):
        self.rotation_counter += 1
        self.wheel1 = "".join([self.wheel1[1:], self.wheel1[0]]) 
        if self.rotation_counter % 26 == 0:
            self.wheel2 = "".join([self.wheel2[1:], self.wheel2[0]]) 
        if self.rotation_counter % 676 == 0:
            self.wheel3 = "".join([self.wheel3[1:], self.wheel3[0]]) 
            self.rotation_counter = 0

    def set_starting_position(self):
        turn1 = ord(self.position[0]) - ord('A')
        self.wheel1 = "".join([self.wheel1[turn1:],self.wheel1[:turn1]])

        turn2 = ord(self.position[1]) - ord('A')
        self.wheel2 = "".join([self.wheel2[turn2:],self.wheel2[:turn2]])

        turn3 = ord(self.position[2]) - ord('A')
        self.wheel3 = "".join([self.wheel3[turn3:],self.wheel3[:turn3]])

class Plugboard:
    def __init__(self, plugs):
        self.plugs_forward = {a:b for a,b in plugs}
        self.plugs_backward = {b:a for a,b in plugs}

    def forward(self, string):
        output = []
        for char in string:
            if char in self.plugs_forward:
                output.append(self.plugs_forward[char])
            else:
                output.append(char)
        return "".join(output)

    def backward(self, string):
        output = []
        for char in string:
            if char in self.plugs_backward:
                output.append(self.plugs_backward[char])
            else:
                output.append(char)
        return "".join(output)

def coding_forward(char, wheel_side_1, wheel_side_2):
    coded_char = ""
    char_ix = wheel_side_1.index(char)
    coded_char = wheel_side_2[char_ix]
    return coded_char

def coding_backward(char, wheel_side_1, wheel_side_2):
    coded_char = ""
    char_ix = wheel_side_2.index(char)
    coded_char = wheel_side_1[char_ix]
    return coded_char


user_input = input()
newEnigma = Enigma(["QOFJSPIRUTNLMXWBZEGYKCADVH","ZHQYASLFCTRPKUWDVBIMJXGOEN", "JQBVTIEUXANMYDKPSWLZHGFROC"],
"YRUHQSLDPXNGOKMIEBFZCWVJAT", "KEY", ["QA", "HJ", "KI", "EV", "GC", "PO", "LK", "NX", "AS", "TE"], user_input)
print(newEnigma.execute())

现在这个“EGJTUABUVJWZCYURYOIDFZZKHZQWGQTTTTTDDFMBNFOQXTSDGANPROIWLLBZJVRXIRQAINSHCURXQLPK”作为一个长字符串应该给出一个英文句子(当然只有大写字母,没有空格)

谢谢你的答案提前!

答案

E输入中有几个插入Aplugboard settings的插头;我认为这可能会产生你的问题:

["QA", "HJ", "KI", "EV", "GC", "PO", "LK", "NX", "AS", "TE"]
  ^A                ^E                            ^A    ^E  

您可能想验证插板输入是否符合要求。

这是一种方法:

def verify_plugs(plug_connections):

    seen = set([letter for pairs in plug_connections for letter in pairs])
    return len(seen) == len(plug_connections) * 2


plug_connections = ["QA", "HJ", "KI", "EV", "GC", "PO", "LK", "NX", "AS", "TE"]
verify_plugs(plug_connections)

output:

False

以上是关于在python中实现enigma构建的插件的问题的主要内容,如果未能解决你的问题,请参考以下文章

Android Gradle 插件组件化中的 Gradle 构建脚本实现 ③ ( 在 Gradle 构建脚本中实现 AndroidManifest.xml 清单文件切换设置 )

vuevue-cli3构建项目中实现图片懒加载

Android Gradle 插件Gradle 自定义 Plugin 插件 ① ( buildSrc 目录中实现 Gradle 插件 | 创建 src/main/groovy 源码目录并编译 )

Android Gradle 插件Gradle 自定义 Plugin 插件 ② ( buildSrc 目录中实现 Gradle 插件 | 实现 Gradle 插件代码 | 模块引入插件并编译 )

使用C语言为python编写动态模块--在C中实现python中的类

Jenkins中实现选项参数联动构建