【发布时间】: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