蛮力字典攻击Caesar密码Python代码在第18班后不工作

2024-04-20 00:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

这是为了使用来自http://www.math.sjsu.edu/~foster/dictionary.txt的字典文件强制执行恺撒密码。它通过三个函数运行,lang_lib()将字典的文本转换为可调用对象,isEnglish()检查短语的百分比,若至少60%的短语和字典中的任何单词匹配,它将返回一个真值。使用该函数,caeser密码函数将运行所有移位,并从英语单词中检查它们。它应该以最高的百分比返回结果,但它似乎只在1-18班工作。我不明白为什么它不起作用

def lang_lib():
    file = open('dictionary.txt', 'r')
    file_read = file.read()
    file_split = file_read.split()
    words = []
    for word in file_split:
        words.append(word)
    file.close()
    return words

dictionary = lang_lib()

def isEnglish(text):
    split_text = text.lower().split()
    counter = 0
    not_in_dict = []
    for word in split_text:
        if word in dictionary:
            counter += 1
        else:
            not_in_dict.append(word)

    length = len(split_text)
    text_percent = ((counter / length) * 100)
    #print(text_percent)
    if text_percent >= 60.0:
        return True
    else:
        return False

alphabet = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%/."

def caeser(text): #Put in text, and it will spit out all possible values
    lower_text = text.lower()
    ciphertext = "" #stores current cipher value
    matches = [] #stores possible matches

    for i in range(len(alphabet)): #loops for the length of input alphabet
        for c in lower_text:
            if c in alphabet:
                num = alphabet.find(c)
                newnum = num - i
                if newnum >= len(alphabet):
                    newnum -= len(alphabet)
                elif newnum < 0:
                    newnum += len(alphabet)
                ciphertext = ciphertext + alphabet[newnum]
            else:
                ciphertext = ciphertext + c

            testing = isEnglish(ciphertext)
            for text in ciphertext:
                if testing == True and len(ciphertext) == len(lower_text):
                    matches.append(ciphertext)
                    return i, matches

        ciphertext = "" #clears ciphertext so it doesn't get cluttered

print(caeser('0x447 #0x$x 74w v0%5')) #shift of 19
print(caeser('zw336 @zw9w 63v uz#4')) #shift of 18

谢谢各位


Tags: textinfordictionarylenreturniflower
2条回答

我发现dictionary.txt不包含2个或3个字母的单词,因此它会用这些单词中的许多单词扭曲长输入,并返回False。我添加了一个常用词列表,因此现在所有输入都能准确地工作

如果有人想帮助我提高代码的效率,我希望能得到一些提示。我对Python非常陌生

根据@tripleee的建议,该部分缩进过深:

testing = isEnglish(ciphertext)
for text in ciphertext:
   if testing == True:
        matches.append(ciphertext)
        return i, matches

此外,如果缩进正确,并且让上一个循环完成,则无需检查长度

相关问题 更多 >