使用由小写字母的所有可能组合构成的键来解密异或字符串的函数

时间:2016-11-20 10:24:40

标签: python encryption xor

我接受了挑战,现在我有点陷入困境。基本上,我得到了一个XORed的ASCII代码列表。我们知道密钥的大小为5,小写英文字母。我已经阅读了不同的解密方法,从频率分析和python工具的使用(如xortool),但我没有设法使它们工作。 Xortool使用了参数-l 5 -o,我遗憾地收到了一百个乱码的结果。

对于这个例子,我将只使用所述ASCII的前5个代码。完整的消息长度为1125.

xor_min = ["49","29","1","67","42"]

这用二进制转换为:

converted_min = ['00110001', '00011101', '00000001', '01000011', '00101010']

然后我制作了一个单独的列表,其中包含字母表中的所有字母,小写字母,二进制形式。

alph = list(string.ascii_lowercase)
bin_key = convert_to_bin((convert_to_ascii(alph)))

到目前为止,一切都很完美。现在我要做的是用bin_key创建一个大小为5的密钥,其中包含26个中所有可能的组合。

key = list(itertools.combinations(bin_key, 5))

确定。现在,key包含65780个元素,位于元组列表中。下一步是对于xor_min中的每个元素,应该使用键列表中的元组进行XOR操作。这是我遇到问题的部分。这是我的全部代码:

def xor_list(xor_encrypted, max_key):
       key = list(itertools.combinations(max_key, 5))
       possible_xor = ['' for x in range(0, len(key))]
       i = 0
       for element in range(0, len(max_key)):
           if i == 5:
                  i = 0
           possible_xor[element] = operator.xor(int(xor_encrypted[i]), int(key[element][i]))
           i+=1
       return possible_xor


alph = list(string.ascii_lowercase)
bin_key = convert_to_bin((convert_to_ascii(alph)))

xor_min = ["49","29","1","67","42"]
converted_min = convert_to_bin(xor_min)

print(xor_list(converted_min,bin_key))

我得到的输出是:[1140048, 1106871, 1100010, 2067215, 1132123, 1140048, 1106871, 1100010, 2067215, 1132452, 1140048, 1106871, 1100010, 2067215, 1140073, 1140048, 1106871, 1100010, 2067215, 1145162, 1140048, 1106871, 1100010, 2067214, 1132122, 1140048, '', '', '', '', '',.........]

我知道我应该将int转换为二进制,但是如果我在operator.xor中使用bin函数,我会收到一条错误消息:

    possible_xor[element] = operator.xor(bin(int(xor_encrypted[i])), bin(int(key[element][i])))
TypeError: unsupported operand type(s) for ^: 'str' and 'str'

如果您有任何问题/需要澄清,请告诉我。此外,如果您有其他/更好的解决方案,请不要害怕分享。通过解密的5个字母的结果,我会浏览它们以查看有意义的内容。它应该是链接或英文单词的开头。

1 个答案:

答案 0 :(得分:0)

首先,您认为itertools.combinations(max_key, 5)足够了吗? 如果密钥包含重复字母,例如" aaaaa&#34 ;?你的方法不是检查这些变种。

但是如果条件说密钥不包含重复字母,那么就可以了,您可以尝试使用此代码查看所有可能的解密变体:

import itertools
import string
encrypted = [100, 101, 102, 103, 104]
alph = list(string.ascii_lowercase)
bin_key = [b for b in map(ord, alph)]
for i in itertools.combinations(bin_key, 5):
    print([chr(i[k] ^ encrypted[k]) for k in range(5)])