我有一个程序如下,该程序基本上比较标准字典中所有可能单词的XOR,并将XORed结果与Ciphertexts的XOR结果进行比较。但是我想复杂度为O(n2)。我不确定如何降低复杂性。

def find_collision():
    a = int("4ADD55BA941FE954",16) ^ int("5AC643BE8504E35E",16)
    with open("/usr/share/dict/words", "r") as f:
        alist = [line.rstrip() for line in f]
    b = len(alist)

    for i in range(0,b,1):
        for j in range(i,b,1):
        if(((int(alist[i].encode('hex'), 16))^ (int(alist[j].encode('hex'), 16)))==a):
            print("Plain Text1: "+alist[i]+'\n'+"Plain Text2: "+alist[j])
            #print "Yes"
            break


任何帮助将非常感激。

最佳答案

首先,让我们尝试简化。

def find_collision():
    key = 0b1000000011011000101100000010000010001000110110000101000001010
    # that's 0x4ADD55BA941FE954^0x5AC643BE8504E35E


然后,我们方便的itertools模块可以为繁重的工作量很大。这将替换嵌套的for循环,并且可能会显着提高工作速度。

from itertools import combinations
##def find_collision()
##    key = 0b1000000011011000101100000010000010001000110110000101000001010
with open("/usr/share/dict/words", "r") as f:
    full_wordlist = combinations( map(str.rstrip,f.readlines()), 2 )
    # Combinations( { ('word1','word2'),('word1','word3'),('word1','word4'),
                    ('word2','word3') ... } )


但是我们不是很在乎整个事情,对吗?我们只关心碰撞,所以让我们做碰撞吧?编辑:由于这里肯定会有文字,所以我们不能转向十六进制,请执行以下操作:

#instead of full_wordlist = combinations(...)

import re
with open("usr/share/dict/words","r") as f:
    words = (word for word in map(str.rstrip,f.readlines()) if not re.search(r"[^0-9a-fA-F]",word))
    # you can avoid the need for regex by doing:
    # words = (word for word in map(str.rstrip,f.readlines()) if
    #         not any(char not in "0123456789abcdefABCDEF" for char in word))
    collisions = [keypair for keypair in combinations(words,2)
                 if bin(int(keypair[0],base=16)^int(keypair[1],base=16)) == key]


然后以理智的方式拉出碰撞,例如:

for collision in collisions:
    print("Collision between {0[0]}^{0[1]} and key".format(collision))

关于python - 降低python中字典暴力的复杂性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22155756/

10-12 14:14
查看更多