首页 > 解决方案 > Cryptopals 挑战 4 个问题

问题描述

我没有得到 Cryptopals 挑战 4 第 1 组的预期结果。

程序的概念是检查这 300 个字符串中的任何一个是否已被单个字符异或。所以用蛮力,我的解决方案是获取每个字符串,将它与键盘上的每个字符进行异或,然后检查这些结果中的任何一个是否会产生一个英文句子。如果不是,则检查下一个字符串。这是我的代码:

MY_DICT = {}
index = 0
my_plaintext = "Now that the party is jumping"

#fills the dictionary with hex strings from the txt file
with open("hexstrings.txt") as f:
    my_list = f.readlines()
    for x in my_list:
        MY_DICT[index] = x.rstrip('\n')
        index = index + 1

i=0
input() #this is just here to help me keep track of where i am when running it

#this loop fills possible_plaintext with all the possible 255 XORs of the i'th string
#of the dictionary that was previously filler from the txt file

for i in range(326):
    possible_plaintexts =  brute_force_singlechar_xor(MY_DICT[i])
    print(possible_plaintexts)
    if possible_plaintexts == my_plaintext:  #line of concern
        print("ya found it yay :) ")

我确定 myBruteForce 函数可以正常工作,因为它在最后一个问题上正常工作,即我对字符串对每个可能的字符进行异或运算。而且我也知道明文是在我看到解决方案之前提供的。我只是不确定为什么我的程序没有识别出明文不在字典中。

(我知道使用评分系统对每个字符串进行评分,看看它是否接近英语会更容易,但这是我现在选择这样做的方式,直到我弄清楚如何让我的评分功能工作/: )

标签: python-3.xcryptography

解决方案


打印时,您的字典“possible_plaintexts”怎么样?你能在印刷文本中找到解决方案吗?它是如何印刷的?

解密后的字符串也应该有一个 '\n' 字符。


推荐阅读