def offering(data): key = mc.srand(16) IV = mc.srand(16) pad = random.randint(5,10) offer = "A" * pad + data + "A" * pad if (random.randint(1,2)) == 1: print "CBC" return mc.encrypt_cbc(key, offer, IV).encode("hex") else: print "ECB" return mc.encrypt_ecb(key, mc.pkcs7(offer, 16)).encode("hex")
def offering(data): key = mc.srand(16) IV = mc.srand(16) pad = random.randint(5, 10) offer = "A" * pad + data + "A" * pad if (random.randint(1, 2)) == 1: print "CBC" return mc.encrypt_cbc(key, offer, IV).encode("hex") else: print "ECB" return mc.encrypt_ecb(key, mc.pkcs7(offer, 16)).encode("hex")
def pkcs7_strip(data, n): if len(data) == 0: raise ValueError('Data cannot be null') fb = ord(data[-1]) if len(data) < fb: raise ValueError('Data cannot be shorter than padding') split = "" ret = "" for i in xrange(len(data) - 1, len(data) - 1 - fb, -1): cur_char = data[i] if ord(cur_char) != fb: raise ValueError('Incorrect value found in padding') split += cur_char ret = data.split(split)[0] if mc.pkcs7(ret, n) != data: raise ValueError('Invalid Padding') return ret
def pkcs7_strip(data, n): if len(data) == 0: raise ValueError('Data cannot be null') fb = ord(data[-1]) if len(data) < fb: raise ValueError('Data cannot be shorter than padding') split = "" ret = "" for i in xrange(len(data)-1, len(data)-1-fb, -1): cur_char = data[i] if ord(cur_char) != fb: raise ValueError('Incorrect value found in padding') split += cur_char ret = data.split(split)[0] if mc.pkcs7(ret, n) != data: raise ValueError('Invalid Padding') return ret
def encrypt_cbc(key, data, IV): #print "ENCRYPT" KEYSIZE = len(IV) data = mc.pkcs7(data, KEYSIZE) num_blocks = len(data) / KEYSIZE fcb = "" for i in range(num_blocks): sb = i*KEYSIZE pb = data[sb:sb+KEYSIZE] #print "PB: ", pb.encode("hex"), len(pb) #print "IV: ",IV.encode("hex"), len(IV) npb = mc.xor_equalLen(pb.encode("hex"), IV.encode("hex")) #if (pb.encode("hex") != mc.xor_equalLen(npb, IV.encode("hex"))): #print "ENCRYPT XOR ERROR!" #print "NPB:", npb, len(npb.decode("hex")) ncb = mc.encrypt_ecb(key, npb.decode("hex")) #print "NCB:", ncb.encode("hex") IV = ncb fcb += ncb #print fcb.encode("hex") #print "---" #print "FCB:",fcb.encode("hex") return fcb
import mccrypto as mc def parse_kv(data): data = data.split("&") pairs = {} for i in xrange(len(data)): kp = data[i].split("=") key = kp[0] val = kp[1] pairs[key] = val return pairs key = mc.srand(16) poison = mc.pkcs7("role=admin", 16) email = "email=aaaaaaaaaa" system = "&uid=0&role=user" system1 = "&uid=1&role=user" user = email + poison + system enc_user = mc.encrypt_ecb(key, mc.pkcs7(user, 16)) treasure = enc_user[16:32] admin = email + "A" * (16 - 7) + system1 e_admin = mc.encrypt_ecb(key, mc.pkcs7(admin, 16)) e_admin = e_admin[:32] + treasure treasure_admin = mc.decrypt_ecb(key, e_admin) print treasure_admin
import mccrypto as mc def parse_kv(data): data = data.split("&") pairs = {} for i in xrange(len(data)): kp = data[i].split("=") key = kp[0] val = kp[1] pairs[key] = val return pairs key = mc.srand(16) poison = mc.pkcs7("role=admin", 16) email = "email=aaaaaaaaaa" system = "&uid=0&role=user" system1 = "&uid=1&role=user" user = email + poison + system enc_user = mc.encrypt_ecb(key, mc.pkcs7(user, 16)) treasure = enc_user[16:32] admin = email + "A"*(16-7) + system1 e_admin = mc.encrypt_ecb(key, mc.pkcs7(admin, 16)) e_admin = e_admin[:32] + treasure treasure_admin = mc.decrypt_ecb(key, e_admin) print treasure_admin
def offering(data, key): return mc.encrypt_ecb(key, mc.pkcs7(data, 16))