Caeser密码暴力破解不会解密-Python

时间:2019-01-15 00:06:45

标签: python brute-force

我试图制作一个用于暴力攻击的程序,该程序将接受凯撒密码消息的输入,并尝试在没有旋转因子的情况下破解它。输出应该是所有26个旋转因子的列表以及相应的消息(例如:KEY#01 :(将输入字符移动1,等等)),其中之一应包含解密后的消息。然后,该消息的轮换号将成为关键(我希望那不会太令人困惑)。这是我的代码:

message = input("Enter a message: ") # user inputs message
    offset = 0

    while offset < 26:
        for char in message:
            decrypt = " "

            if not char.isalpha(): # to keep punctuation unchanged
                decrypt = decrypt + char

            elif char.isupper():
                decrypt = decrypt + chr((ord(char) - offset - 65) % 26 + 65) # -65 +65 for uppercase Z

            else:
                decrypt = decrypt + chr((ord(char) - offset - 97) % 26 + 97) # -97 +97 for lowercase z

            offset = offset + 1
            print ("KEY #:", offset, message)

程序不会解密输入,并且由于某种原因它会打印41次相同的输入...我不是一个非常熟练的程序员,所以如果有人可以帮助我,那将是很好的选择。

2 个答案:

答案 0 :(得分:1)

您的脚本存在三个问题:offset = offset + 1在您的for循环中,因此offset对于每个不需要的字符都会增加。并且decrypt变量的初始化也位于循环内。最后,您要打印的是message,而不是decrypt的结果。

这确实有效:

message = input("Enter a message: ")  # user inputs message
offset = 0

while offset < 26:
    decrypt = ""
    for char in message:
        if not char.isalpha():  # to keep punctuation unchanged
            decrypt = decrypt + char

        elif char.isupper():
            decrypt = decrypt + chr((ord(char) - offset - 65) % 26 + 65)  # -65 +65 for uppercase Z

        else:
            decrypt = decrypt + chr((ord(char) - offset - 97) % 26 + 97)  # -97 +97 for lowercase z

    offset = offset + 1
    print("KEY #:", offset, decrypt)

答案 1 :(得分:0)

message = input("Enter a message: ") # user inputs message
    offset = 0 # setting offset to 0

    for offset in range(len(message)): # for loop to run as many times as length of message

        decrypt = " "
        for char in message:

            if not char.isalpha(): # to keep punctuation unchanged
                decrypt = decrypt + char

            elif char.isupper():
                decrypt = decrypt + chr((ord(char) - offset - 65) % 26 + 65) # -65 +65 for uppercase Z

            else:
                decrypt = decrypt + chr((ord(char) - offset - 97) % 26 + 97) # -97 +97 for lowercase z

        print ("KEY #:", offset, decrypt)