def genk1(k, s): res = [k] tmp = k tmp = transduce(tmp) res.append(tmp) tmp = transduce(tmp, s) res.append(tmp) # print 'k1', res return res
def genk1(k): res = [k] tmp = k tmp = transduce(tmp, gets(kk1[0][:32])) res.append(tmp) tmp = transduce(tmp, 0) res.append(tmp) # print 'k1', res return res
def mid1(p, k, s0): p = xor(p, kk1[0][:32]+k[0]) p = transduce(p)[32:] p = xor(p, k[1]) p = transduce(p) p = xor(p, k[2]) p = transduce(p, s0) res = bin2block(p) # print res return res
def solve(ct, pt): global mk1, mk2, kk1, kk2 # print kk1, kk2 # global kk1, kk2, key tmp_mk1 = [] tmp_mk2 = [] testp = map(lambda x: block2bin(x)[:len(kk1[0])+n], pt) testc = map(lambda x: block2bin(x)[32:len(kk2[0])+n], ct) m = {} for ind, k2 in enumerate(mk2): if k2 is None: # print 'well' continue for i in range(6): mid = map(lambda x: mid2(x, k2, i), testc) mid = tuple(mid) if m.has_key(mid): m[mid].append((k2, i, ind)) else: m[mid] = [(k2, i, ind)] # if k2 == kkk2: # print 'k2', mid print 'm OK', len(m) cnt = 0 for ind, k1 in enumerate(mk1): if k1 is None: continue for i in range(6): mid = map(lambda x: mid1(x, k1, i), testp) mid = tuple(mid) # if k1 == kkk1: # print 'k1', mid if m.has_key(mid): if k1[0] == [0] * 8: # print 'k1', k1 # print 'k2', k2 pass for k2, ii, ind2 in m[mid]: if transduce(k1[-1], gets(kk1[2][:32])) == k2[-1]: tmp_mk1.append((k1, ind)) tmp_mk2.append((k2, ind2)) cnt += 1 # print k1, i, k2, ii, jj # break print 'filter mk', len(tmp_mk1), len(tmp_mk2) ''' assert [i for i in tmp_mk1 if i[0] == kk1] assert [i for i in tmp_mk2 if i[0] == kk2] print 'pass assert!' ''' mk1 = [None] * (2**n) mk2 = [None] * (2**n) for k1, ind1 in tmp_mk1: mk1[ind1] = k1 for k2, ind2 in tmp_mk2: mk2[ind2] = k2 print len([i for i in mk1 if i is not None]), len([i for i in mk2 if i is not None])
def mid1(p, k, s0): for i in range(3): s = 0 p = xor(p, k[i]) if i == 1: s = s0 p = transduce(p, s) res = bin2block(p) # print res return res
[0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1]]), ([[1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1], [1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]], [[1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0]])] # ''' cnt = 0 while queue: cnt += 1 kk1, kk2 = queue.pop(0) res = final(kk1, kk2) if res: print 'HAHA!' k1a, k1b, k2a, k2b = res print k1a print k2a print k1b print k2b if transduce(k1a[-1]) == k2b[-1]: queue.append((k1a, k2b)) queue.append((k1b, k2a)) else: queue.append((k1a, k2a)) queue.append((k1b, k2b)) if cnt == 10: break print queue
def solve(ct, pt, kk1, kk2): global mk1, mk2 # print kk1, kk2 # global kk1, kk2, key tmp_mk1 = [] tmp_mk2 = [] testp = map(lambda x: block2bin(x)[:len(kk1[0])+n], pt) testc = map(lambda x: block2bin(x)[:len(kk2[0])+n], ct) m = {} for ind, k2 in enumerate(mk2): if k2 is None: # print 'well' continue for i in range(6): for j in range(6): mid = map(lambda x: mid2(x, k2, i, j), testc) mid = tuple(mid) if m.has_key(mid): m[mid].append((k2, i, j, ind)) else: m[mid] = [(k2, i, j, ind)] # if k2 == kkk2: # print 'k2', mid print 'm OK', len(m) cnt = 0 for ind, k1 in enumerate(mk1): if k1 is None: continue for i in range(6): mid = map(lambda x: mid1(x, k1, i), testp) mid = tuple(mid) # if k1 == kkk1: # print 'k1', mid if m.has_key(mid): if k1[0] == [0] * 8: # print 'k1', k1 # print 'k2', k2 pass for k2, ii, jj, ind2 in m[mid]: if transduce(k1[-1]) == k2[-1]: tmp_mk1.append((k1, ind)) tmp_mk2.append((k2, ind2)) cnt += 1 # print k1, i, k2, ii, jj # break print 'filter mk', len(tmp_mk1), len(tmp_mk2) ''' assert [i for i in tmp_mk1 if i[0] == kk1] assert [i for i in tmp_mk2 if i[0] == kk2] print 'pass assert!' ''' mk1 = [None] * 6 * (2**n) mk2 = [None] * 6 * (2**n) for k1, ind1 in tmp_mk1: mk1[ind1] = k1 for k2, ind2 in tmp_mk2: mk2[ind2] = k2 print len([i for i in mk1 if i is not None]), len([i for i in mk2 if i is not None])
# solve(cts, pts) return [i for i in mk1 if i is not None] + [i for i in mk2 if i is not None] kk1 = [[1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0], [1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0]]; kk2 = [[1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1]] queue = [(kk1, kk2)] while queue: kk1, kk2 = queue.pop(0) res = final() if res: print 'HAHA!' k1a, k1b, k2a, k2b = res print k1a print k2a print k1b print k2b if transduce(k1a[-1], gets(kk1[2][:32])) == k2b[-1]: k1a = [i[:32]+j for i,j in zip(kk1, k1a)] k1b = [i[:32]+j for i,j in zip(kk1, k1b)] k2a = [i[:32]+j for i,j in zip(kk2, k2a)] k2b = [i[:32]+j for i,j in zip(kk2, k2b)] queue.append((k1a, k2b)) queue.append((k1b, k2a)) elif transduce(k1a[-1], gets(kk1[2][:32])) == k2a[-1]: k1a = [i[:32]+j for i,j in zip(kk1, k1a)] k1b = [i[:32]+j for i,j in zip(kk1, k1b)] k2a = [i[:32]+j for i,j in zip(kk2, k2a)] k2b = [i[:32]+j for i,j in zip(kk2, k2b)] queue.append((k1a, k2a)) queue.append((k1b, k2b)) else: assert 0