【问题标题】:When decrypting caesar cipher via brute force, cannot print right attempts通过暴力破解凯撒密码时,无法打印正确的尝试
【发布时间】:2017-11-02 14:47:30
【问题描述】:

我正在尝试通过暴力破解凯撒密码。我可以很容易地加密某些东西,然后我希望程序使用蛮力解密消息。我想要发生的是让 python 打印出加密消息的所有 26 个移位值。这是我的代码:

message = input("What message do you want to use: ")
shiftValue = int(input("What would you like to shift the message by: "))
encryptedMsg = ""

for character in message: 
    if character.isalpha() == True: 
        if character == character.lower():
            x = ord(character) - 97 
            x += shiftValue 
            x = x % 26  
            encryptedMsg += chr(x + 97) 
        else:
            x = ord(character) - 65
            x += shiftValue
            x = x % 26
            encryptedMsg += chr(x+65)
    else: 
        encryptedMsg += character

print(encryptedMsg)

def decrypt(encryptedMsg):
    i = 0
    shiftValue = 0
    while i < 26:                  
        attempt = ""
        for char in encryptedMsg:
            if char.isalpha() == True:
                x = ord(char) - 97
                x = x + shiftValue
                x = x % 26
                attempt += chr(x+97)
            else:
                attempt += char
            print(attempt)
            i += 1
            shiftValue += 1

decrypt(encryptedMsg)

运行此程序后,我会在 python shell 上获得以下代码。假设消息变量是“我的名字是丹尼尔”,我使用的 shiftValue 为 2。这就是打印的内容:

i
ib
ib 
ib s
ib sg
ib sgt
ib sgtm
ib sgtm 
ib sgtm s
ib sgtm sd
ib sgtm sd 
ib sgtm sd k
ib sgtm sd ko
ib sgtm sd koc
ib sgtm sd kocy
ib sgtm sd kocyv
ib sgtm sd kocyvd
z
zs
zs 
zs j
zs jx
zs jxk
zs jxkd
zs jxkd 
zs jxkd j
zs jxkd ju
zs jxkd ju 
zs jxkd ju b
zs jxkd ju bf
zs jxkd ju bft
zs jxkd ju bftp
zs jxkd ju bftpm
zs jxkd ju bftpmu

【问题讨论】:

    标签: python-3.x encryption brute-force caesar-cipher


    【解决方案1】:

    decrypt() 的最后 3 行在 for char in encryptedMsg 的每次迭代中执行。这是错误的。您想在打印之前完成解密字符串的创建。

    另一个问题是您的程序没有正确处理大写字符。一个快速的解决方法是在处理之前使用lower() 将所有内容转换为小写。

    试试这个:

    def decrypt(encryptedMsg):
        i = 0
        shiftValue = 0
        while i < 26:                  
            attempt = ""
            for char in encryptedMsg.lower():
                if char.isalpha() == True:
                    x = ord(char) - 97
                    x = x + shiftValue
                    x = x % 26
                    attempt += chr(x+97)
                else:
                    attempt += char
            i += 1
            shiftValue += 1
            print(attempt)
    

    编辑:

    实现循环的一种更“pythonic”的方式是使用像for x in range(y): 这样的语法。此外,if x == True 始终可以简化为 if x:。这是带有单个迭代器变量 (shiftValue) 的代码的简化版本:

    def decrypt(encryptedMsg):
        for shiftValue in range(26):
            attempt = ""
            for char in encryptedMsg.lower():
                if char.isalpha():
                    x = (ord(char) - 97 + shiftValue) % 26
                    attempt += chr(x+97)
                else:
                    attempt += char
            print(attempt)
    

    【讨论】:

    • 谢谢。我已经编辑了我的代码,以便它现在可以加密大小写字母,但是当我运行它时,Python 在打印出 26 个解决方案时只会打印出空白语句。你知道为什么吗?查看已编辑的第一篇文章
    • 没有。但在if char.isalpha(): 之后,尝试添加类似if char.isupper(): base = ord('A') else: base = ord('a') 的内容,然后在以下几行中将97 替换为base
    • 谢谢!解决了问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    相关资源
    最近更新 更多