Esempio n. 1
0
def encrypt_database_0(path_, file_, file_e_): # n is the nb of seqs, m is the length of a seq 
    # read the public key from stored file
    buf=open(path_+"pub.txt",'rb')
    pub=pickle.load(buf)
    buf.close()
    in_ = open(path_+file_,'r')
    n,m = in_.readline().split()
    n=int(n)
    m=int(m)
    x=0
    out_= open(path_+file_e_,'w')
    out_.write("%s %s\n" %(n, 2*m))
    for line in in_:
        #print line 
        for char in line:
            if char =='A':
                #encrypt 00 
                out_.write("%s\n" % encrypt(pub, 0))
                out_.write("%s\n" % encrypt(pub, 0))
            elif char=='C':
                #encrypt 01 
                out_.write("%s\n" % encrypt(pub, 0))
                out_.write("%s\n" % encrypt(pub, 1))
            elif char=='G':
                #encrypt 10 
                out_.write("%s\n" % encrypt(pub, 1))
                out_.write("%s\n" % encrypt(pub, 0))
            elif char=='T':
                #encrypt 11 
                out_.write("%s\n" % encrypt(pub, 1))
                out_.write("%s\n" % encrypt(pub, 1))
    out_.close()
    in_.close()
    return file_e_
def pre_compute(path_):
    buf=open(path_+"pub.txt",'rb')
    pub=pickle.load(buf)
    buf.close()
    with open(path_+file_pre, 'w') as out:
        for k in range(1,101): 
            out.write("%s\n"%encrypt(pub, pub.n-k))
        out.close()
Esempio n. 3
0
def encrypt_database_1(path_, file_, file_e_): 
    # read the public key from stored file
    buf=open(path_+"pub.txt",'rb')
    pub=pickle.load(buf)
    buf.close()

    in_ = open(path_+file_,'r')
    n,m = in_.readline().split()
    n=int(n)
    m=int(m)
    out_= open(path_+file_e_,'w')
    out_.write("%s %s\n" %(n, 4*m))
    for line in in_:
        y=0
        for char in line.strip():
            if char =='A':
                out_.write("%s\n"%encrypt(pub, 1))
            else:
                out_.write("%s\n"%encrypt(pub, 0))
            if char =='C':
                out_.write("%s\n"%encrypt(pub, 1))
            else:
                out_.write("%s\n"%encrypt(pub, 0))
            if char =='G':
                out_.write("%s\n"%encrypt(pub, 1))
            else:
                out_.write("%s\n"%encrypt(pub, 0))
            if char =='T':
                out_.write("%s\n"%encrypt(pub, 1))
            else:
                out_.write("%s\n"%encrypt(pub, 0))
#    for x in hA: 
#        print x 

    out_.close()
    in_.close()
    return file_e_
def handle_query_1(query, path_, file_e_):
    in_=open(path_+file_e_, 'r')
    n, m =in_.readline().split()
    n=int(n)
    m=int(m)/3
    buf=open(path_+"pub.txt",'rb')
    pub=pickle.load(buf)
    buf.close()
#    buf=open(path_+"priv.txt",'rb')
#    priv=pickle.load(buf)
#    buf.close()
#    print query 
    query.sort(key=lambda x: x[1])
#    print query
    res=[0]*n #equal to the number of seq 
    significant_time_=[0]*n
#    counter=0
    cnst_1_= encrypt(pub, 1)
    with open(path_+file_pre, 'r') as pre_: 
            cnst= mpz(pre_.readlines()[len(query)-1])
            pre_.close()
    for i in range(n):
        acc=1
        old_pos=0
        for (letter, pos) in query:
            for j in range(old_pos, pos):
                in_.readline()
                in_.readline()
                in_.readline()
            
#                counter+=4
#            print counter
            old_pos=pos+1
            s1_=mpz(in_.readline())
            s2_=mpz(in_.readline())
            s3_=mpz(in_.readline())
            if letter=='A': 
                st_=time.time()
                acc= e_add(pub, acc, s1_)
                significant_time_[i]+=time.time()-st_
            elif letter=='C':
                st_=time.time()
                acc= e_add(pub, acc, s2_)
                significant_time_[i]+=time.time()-st_
            elif letter=='G':
                st_=time.time()
                acc= e_add(pub, acc, s3_)
                significant_time_[i]+=time.time()-st_
            elif letter=='T':
                st_=time.time()
                acc= e_add(pub, acc, e_add(pub, cnst_1_, invert(e_add(pub,e_add(pub, s1_, s2_),s3_), pub.n_sq)))
                significant_time_[i]+=time.time()-st_
#            counter+=4
#            print counter 
        for j in range(3*pos+3, 3*m):
            in_.readline()
#            counter+=1
#        print counter
        st_=time.time()
        res[i]=e_mul_const(pub, e_add(pub, acc, cnst), random.randrange(pub.n))
        significant_time_[i]+=time.time()-st_
        #print significant_time_[i]
    in_.close()
    sg=sum(significant_time_)
    print "sum significant query time = %f s" % (sg)
    return decrypt_query_res(res, path_), sg