蛮力字典攻击凯撒密码Python代码在第18个班次后无法正常工作

问题描述 投票:-3回答:2

[这是使用http://www.math.sjsu.edu/~foster/dictionary.txt中的词典文件进行的强奸凯撒密码。它通过三个函数运行:lang_lib()使字典的文本成为可调用的对象; isEnglish(),检查短语的百分比,以及是否至少有60%与字典中的任何单词匹配,它将返回True值。使用此功能,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

谢谢你们。

python encryption caesar-cipher
2个回答
0
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.