In [1]:
#foo=bar&baz=qux&zap=zazzle
def parse2dict(s):
    d={}
    s=s.split("&")
    for item in s:
        t=item.split("=")
        d[t[0]]=t[1]
    return d

In [2]:
parse2dict("foo=bar&baz=qux&zap=zazzle")


Out[2]:
{'baz': 'qux', 'foo': 'bar', 'zap': 'zazzle'}

In [25]:
uid=1
def profile_for(key,email):
    global uid
    email=email.strip("&=")
    s="email="+str(email)+"&uid="+str(uid)+"&role=user"
    news=pad(s,16)
    out=encryptECBAES(key,news)
    #uid+=1
    return out

def create_profile(key,ct):
    out=decryptECBAES(key,ct)
    out=unpad(out)
    d=parse2dict(out)
    return d

#print profile_for("foo@bar.com")
#{
#  email: 'foo@bar.com',
#  uid: 10,
#  role: 'user'
#}

In [4]:
#Now, two more easy functions. Generate a random AES key, then:

#Encrypt the encoded user profile under the key; "provide" that to the "attacker".
#Decrypt the encoded user profile and parse it.

In [5]:
from blockcrypto import *

In [10]:
globalkey=randomkey(16)

In [11]:
ct=profile_for(globalkey,"foo@bar.com")

In [12]:
print create_profile(globalkey,ct)


{'role': 'user', 'email': 'foo@bar.com', 'uid': '2'}

In [9]:
print len(ct)


48

In [26]:
#now cannot only look at first block, instead find #bytes to add between 1 block staying the same, and 2 blocks staying the same

#rework code - add 2*blocksize bytes iteratively - do any blocks stay the same at any point
def newfindblocksize(blackbox,key, limit):
    firstblocknum=None
    for bs in xrange(1,limit):
        for byte in xrange(bs,2*bs):
            ct=blackbox(key,"A"*byte)
            blocks=splitblocks(ct,bs)
            #print blocks
            for i in xrange(len(blocks)):
                try:
                    if blocks[i] == prevset[i]:
                        #print prevset
                        #print blocks
                        #print ascii2hexstring(block)
                        return bs
                except:
                    pass
            prevset=list(blocks)
    return None

In [27]:
### fuck it, assume 128 bit key, otherwise we need to look at how many bytes we have to insert between one block which was changing stopping changing, and two

import numpy as np
def newfindblocksize(blackbox,key, limit):
    firstblocknum=None
    for bs in xrange(1,limit):
        origct=blackbox(key,"")
        hasblockchanged=np.zeros(l)
        for byte in xrange(bs,2*(bs+1)):
            ct=blackbox(key,"A"*byte)
            blocks=splitblocks(ct,bs)
            #print blocks
            try:
                if np.sum(np.array(blocks)==np.array(prevset)) == 1:
                    firstnum=byte
                elif np.sum(np.array(blocks)==np.array(prevset)) >=2:
                    print bs, byte, firstnum
                    print byte-firstnum
                    print "----"
                    break
            except:
                pass
            prevset=list(blocks)
    return None
print newfindblocksize(profile_for,globalkey,256)

In [ ]:


In [ ]: