【问题标题】:Python Caesar breakPython凯撒破解
【发布时间】:2012-11-21 16:48:30
【问题描述】:

我们将为 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']

【问题讨论】:

  • 你尝试了什么?你被困在哪里了?
  • def Caesar_break(cipher:str) ->str: 有效的 Python 吗?
  • @hayden:是的,虽然它在这里不是很有用。请参阅PEP 3107ref
  • @Anony-Mousse 这是我目前得到的结果,我试图让它只打印“嘿”,但它总是显示为空白,所以我回到了以前的状态。
  • 我需要帮助才能将其中一个结果与字典中的单词相匹配。

标签: python python-3.x decoding encryption


【解决方案1】:

解决这个问题的规范方法是:

  1. 从单词列表中读取所有行并将它们插入字典dict
  2. 通过translate尝试各种凯撒旋转
  3. 如果旋转产生字典中的单词 (decrypt in dictionary),则输出它

对于较长的文本,您可能需要检查例如至少有一半的词在字典中找到。注意小写/大写问题和换行字符等! - 尝试使用strip

更高级的方法是进行字符统计,并根据该统计信息猜测适当的旋转。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多