コード例 #1
0
def distinguishing(ct, tot):
    cnt = 0
    for i in range(tot):
        ct0 = str_to_bits(ct[32*i:32*i+16].decode('hex'))
        ct1 = str_to_bits(ct[32*i+16:32*i+32].decode('hex'))
        p0 = ct0[41]^ct0[1]^ct0[19]^ct0[63]^ct0[60] # the bits after permuting 2104008000008000
        p1 = ct1[41]^ct1[1]^ct1[19]^ct1[63]^ct1[60]
        if p0==p1:
            cnt += 1
    ratio = float(cnt)/tot
    print ratio
    if ratio > 0.508:
        return True
    else:
        return False
コード例 #2
0
ファイル: decrypt.py プロジェクト: gartea/rpi_crypto_hw1
import des
import sys


if __name__ == '__main__':
    infile = sys.argv[1]
    outfile = sys.argv[2]
    keyfile = sys.argv[3]

    keyf = open(keyfile)
    key = keyf.read()
    keyf.close()

    keys = des.create_keys(key)
    keys.reverse()

    inf = open(infile)
    inbits = des.str_to_bits(inf.read())
    outbits = [des.round_runner(x, keys) for x in inbits]
    chars = des.bits_to_str(outbits)
    outf = open(outfile, 'w+')
    outf.write(chars)
    outf.close
コード例 #3
0
def recover(diff, sdiff0, sdiff1, num, pos, known):
    diff = str_to_bits(diff)
    diff2 = [diff[x-1] for x in IP_1]
    diff2 = bits_to_str(diff2)
    diff2 = int(diff2.encode('hex'),16)
    #print hex(diff2)
    
    p = ''
    ct = ''
    for i in range(num):
        pt = random.randrange(0, 2**64)
        for x in sdiff1:
            p += p64(pt^x, endian='big')
        for x in sdiff1:
            p += p64(pt^x^diff2, endian='big')
    
    for i in range((len(p)-1)/10000+1):
        c.sendlineafter('(hex):',p[i*10000:(i+1)*10000].encode('hex'))
        tmp = c.recvline().strip()
        ct += tmp
    
    ss = len(sdiff0)
    rr = []
    for i in range(num):
        bits = str_to_bits(p[i*ss*16:i*ss*16+8])
        bits = [bits[x-1] for x in IP]
        rr.append(bits[32:64])
        bits = str_to_bits(p[i*ss*16+ss*8:i*ss*16+ss*8+8])
        bits = [bits[x-1] for x in IP]
        rr.append(bits[32:64])
    
    '''
    file format:
    len(sdiff0)
    sdiff0 (array)
    num = len(rr)
    rr (array)
    ct
    pos
    known kbits
    '''
    fw = open('dat','w')
    print >>fw, len(sdiff0)
    for x in sdiff0:
        fw.write(str(x)+' ')
    fw.write('\n')
    print >>fw, num
    for i in range(2*num):
        print >>fw, bits_to_str(rr[i]).encode('hex')
    print >>fw, ct
    print >>fw, len(pos)-1
    for i in range(len(pos)-1):
        fw.write(str(pos[i])+' ')
    fw.write('\n')
    print >>fw, len(known)
    for i in range(len(known)):
        fw.write(str(known[i][0])+' '+str(known[i][1])+' ')
    fw.write('\n')
    fw.close()
    
    os.system("./util r > tmpres")
    f = open('tmpres')
    res = []
    for line in f:
        l = line.strip()
        if l!='':
            res.append(map(int,l.split(' ')))
    f.close()
    return res
コード例 #4
0
def subsets(s):
    bits = str_to_bits(s)
    return gen_subsets(bits, 0)