我们将为 26 个可能的密钥中的每一个生成解密字母。
我完成了那部分,但我无法打印出字典中的单词。例如:我只想打印 'ebv' 嘿
我如何将它与字典中的单词匹配,而不是打印出所有 26 个可能的键。
这是字典: http: //www.ics.uci.edu/~kay/wordlist.txt
Alphabet = 'abcdefghijklmnopqrstuvwxyz'
def rotated_alphabet(key:int) -> str:
'''produces a rotated alphabet based on key and adds those letters to the end of alphabet'''
if key > 26:
key = key % 26
new_alphabet = ''
w = Alphabet[0:key]
x = Alphabet.replace(Alphabet[0:key], new_alphabet)
return (x + w)
def Caesar_break(cipher:str) ->str:
infile = open('wordlist.txt', 'r')
wordlist = []
possible = []
decode = []
words = []
for str in infile:
t = str
for i in range(0, 26):
p = rotated_alphabet(i).split()
possible+=p
for y in possible:
decrypt = str.maketrans(y, Alphabet)
decode.append(cipher.translate(decrypt))
for str in decode:
s = str
words.append(s)
print(words)
Caesar_break('eby')
它打印出来:
['ebv', 'dau', 'czt', 'bys', 'axr', 'zwq', 'yvp', 'xuo', 'wtn', 'vsm', 'url', 'tqk', 'spj', 'roi', 'qnh', 'pmg', 'olf', 'nke', 'mjd', 'lic', 'khb', 'jga', 'ifz', 'hey', 'gdx', 'fcw']