作用:在对密文进行暴力破解时,加密的消息有成千上万个密钥,计算机虽然仍可以轻易的遍历所有可能,人却需要检查数千个解密得到的消息以识别出正确的明文。可以想象这得有多烦!!
当计算机使用错误的密钥解密消息时,得到的字符串是垃圾文本而不是英语文本,可以通过写一个小脚本来识别信息何时是英语,从而避免必须肉眼查看成千上万个不正确解密的苦差
需要创建一个python函数,传入一个字符串,如果是有效文本,则返回真,如果是随机码则返回false,英语句是由词典中的单词组成的,但垃圾文本不是,单词间通常由空格分割,所以可以根据空格将消息分割成更短的字符串,检查他们是否为词典中的词
加载词典(dictionary.txt是一个涵盖大部分英语单词的文件,我已上传到我的资源,不需要积分下载!!!)打开文件以“换行符”为分割,将词典中的词作为key传入创建的字典englishwords中,key可以理解为一个索引,这样就能加快查询的速度。(想想,为什么不用列表呢?),最后将字典返回,单词库就创建好了
UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
def loadDictionary():
dictionaryFile = open('dictionary.txt') #词典的位置
englishWords = {}
for word in dictionaryFile.read().split('\n'):
englishWords[word] = None
dictionaryFile.close()
return englishWords
ENGLISH_WORDS = loadDictionary()
对消息中出现的英文单词进行计数
接收一个字符参数并放回一个结果,代表识别的单词中是英语单词与总词个数的比值,比值为0~1的值,为0意味着没有一个词是英文单词。1表示所有的单词都是英语单词
def getEnglishCount(message):
message = message.upper()
message = removeNonLetters(message) #removeNonLetters()是移除非字母字符的函数,后面会写到
possibleWords = message.split() #按空格分割为列表形式存储在possiblewords中
if possibleWords == []:
return 0.0 #没有一个是单词
matches = 0 #统计
for word in possibleWords:
#使用for循环对每个单词进行迭代并检查是否存在字典中
if word in ENGLISH_WORDS:
matches += 1
return float(matches) / len(possibleWords) #返回比例
删除非字母字符
数字或标点符号会让分割出来的单词跟字典文件里的单词匹配不上,会引起检测的问题,比如 “love.”,没有删除字符串结尾的句号,那么它就不会算作是英文单词,为了避免该问题,需要删除数字和标点符号
def removeNonLetters(message):
lettersOnly = []
for symbol in message:
#LETTERS_AND_SPACE是开头创建的包含所有字母和\n\t的字符串(不包含标点)
if symbol in LETTERS_AND_SPACE:
lettersOnly.append(symbol)
return ''.join(lettersOnly)
计算百分比
计算message字符串的各百分比,例如 “i am very handsome ash afhsjhfj asfhk afaf”中有8个字母组合,但是只有4个是单词。如果一个字符串中有20%的单词存在于英语词典文件中,并且85%的字符是字母和空格,那么就可以判断该字符串是英语。
def isEnglish(message, wordPercentage=20, letterPercentage=85):
wordsMatch = getEnglishCount(message) * 100 >= wordPercentage
numLetters = len(removeNonLetters(message))
messageLettersPercentage = float(numLetters) / len(message) * 100
lettersMatch = messageLettersPercentage >= letterPercentage
return wordsMatch and lettersMatch
运行效果
英语字典下载地址
https://download.csdn.net/download/weixin_45783299/23375543
detectEnglish.py源代码
UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
def loadDictionary():
dictionaryFile = open('dictionary.txt')
englishWords = {}
for word in dictionaryFile.read().split('\n'):
englishWords[word] = None
dictionaryFile.close()
return englishWords
ENGLISH_WORDS = loadDictionary()
def getEnglishCount(message):
message = message.upper()
message = removeNonLetters(message)
possibleWords = message.split()
if possibleWords == []:
return 0.0
matches = 0
for word in possibleWords:
if word in ENGLISH_WORDS:
matches += 1
return float(matches) / len(possibleWords)
def removeNonLetters(message):
lettersOnly = []
for symbol in message:
if symbol in LETTERS_AND_SPACE:
lettersOnly.append(symbol)
return ''.join(lettersOnly)
def isEnglish(message, wordPercentage=20, letterPercentage=85):
wordsMatch = getEnglishCount(message) * 100 >= wordPercentage
numLetters = len(removeNonLetters(message))
messageLettersPercentage = float(numLetters) / len(message) * 100
lettersMatch = messageLettersPercentage >= letterPercentage
return wordsMatch and lettersMatch