【问题标题】:Caeser Cipher Brute Force Attack Won't Decrypt - Python凯撒密码蛮力攻击不会解密 - Python
【发布时间】:2019-01-15 00:06:44
【问题描述】:

我尝试编写一个用于暴力攻击的程序,该程序将接受凯撒加密消息的输入,并尝试在没有轮换因素的情况下破解它。输出应该是所有 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 次...我不是一个非常熟练的程序员,所以如果有人可以帮助我,那就太好了。

【问题讨论】:

    标签: python brute-force


    【解决方案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)
    

    【讨论】:

      【解决方案2】:
      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        相关资源
        最近更新 更多