def decryptMAABE(readFileGP, readFileUserSecretKey, readFileCipherText): pickleFileGP = pickleLoad(readFileGP) gp = byToOb(pickleFileGP) gp.update({'H': H, 'F': F}) pickleFileUserSecretKey = pickleLoad(readFileUserSecretKey) sk = byToOb(pickleFileUserSecretKey) pickleFileCipherText = pickleLoad(readFileCipherText) ct = byToOb(pickleFileCipherText) policy = util.createPolicy(ct['policy']) coefficients = util.getCoefficients(policy) pruned_list = util.prune(policy, sk['keys'].keys()) if not pruned_list: raise Exception( "You don't have the required attributes for decryption!") B = group.init(GT, 1) for i in range(len(pruned_list)): x = pruned_list[i].getAttribute() # without the underscore y = pruned_list[i].getAttributeAndIndex() # with the underscore B *= (ct['C1'][y] * pair(ct['C2'][y], sk['keys'][x]['K']) * pair(ct['C3'][y], gp['H'](sk['GID'])) * pair(sk['keys'][x]['KP'], ct['C4'][y]))**coefficients[y] print("Decrypt") # print("SK:") # print(sk) print("Decrypted AES Key using MAABE:") print(ct['C0'] / B) return ct['C0'] / B
def predecrypt(pk, ik, ct): group = PairingGroup('SS512') util = SecretUtil(group, debug) w = pk['w'] u = pk['u'] S = ik['S'] #['THREE', 'ONE', 'TWO'] K0 = ik['K0'] K1 = ik['K1'] Kj2 = ik['Ki2'] Kj3 = ik['Ki3'] C0 = ct['C0'] Ci1 = ct['C_j_1'] Ci2 = ct['C_j_2'] Ci3 = ct['C_j_3'] Ci4 = ct['C_j_4'] Ci5 = ct['C_j_5'] policy = util.createPolicy(ct['policy']) #((ONE or THREE) and (TWO or FOUR)) print('policy in predecrypt===', policy) print('S in predecrypt===', S) pruned = util.prune(policy, S) #[ONE, TWO] if pruned is False: raise Exception('Prune error in pre-decrypt') wi = util.getCoefficients(policy) #{u'TWO': <pairing.Element>, u'FOUR': <pairing.Element>, u'THREE': <pairing.Element>, u'ONE': <pairing.Element>} eC0K0 = pair(C0,K0) ECi4wi = 0 for each in pruned: i = each.getAttribute() ECi4wi += Ci4[i] * wi[i] ewECi4wiK1 = pair(w ** ECi4wi,K1) PIeCi1K1eCi2uCi5Kj2eCi3Kj3wi = 1 for each in pruned: j = each.getAttributeAndIndex() i = each.getAttribute() eCi1K1 = pair(Ci1[i],K1) eCi2uCi5Kj2 = pair(Ci2[i] * (u ** -Ci5[i]), Kj2[j]) eCi3Kj3 = pair(Ci3[i],Kj3[j]) PIeCi1K1eCi2uCi5Kj2eCi3Kj3wi *= (eCi1K1 * eCi2uCi5Kj2 * eCi3Kj3) ** wi[i] return objectToBytes(eC0K0/(ewECi4wiK1 * PIeCi1K1eCi2uCi5Kj2eCi3Kj3wi), group)
def setup(self): g = group.random(G1) p = group.random(G1) h = group.random(G1) u = group.random(G1) v = group.random(G1) w = group.random(G1) alpha = group.random() e_gg_alpha = pair(g,g) ** alpha pk = {'p':objectToBytes(p, group), 'g':objectToBytes(g, group), 'h':objectToBytes(h, group), 'u':objectToBytes(u, group), 'v':objectToBytes(v, group), 'w':objectToBytes(w, group), 'eggalpha':objectToBytes(e_gg_alpha, group) } msk = {'pk':pk, 'alpha':objectToBytes(alpha, group)} return {'pk':pk, 'msk':msk}
def pair_prod(self, lhs, rhs): """takes two lists of G1 & G2 and computes a pairing product""" return pair(lhs, rhs, self.Pairing)
def __randomGT(self): if not hasattr(self, 'gt'): self.gt = pair(self.random(G1), self.random(G2)) z = self.random(ZR) return self.gt ** z
def pair_prod(self, lhs, rhs): return pair(lhs, rhs, self.Pairing)
def setup(): g1 = group.random(G1) g2 = group.random(G2) egg = pair(g1, g2) gpWithoutHF = {'g1': g1, 'g2': g2, 'egg': egg} return gpWithoutHF
C0 = ct['C0'] Ci1 = ct['C_j_1'] Ci2 = ct['C_j_2'] Ci3 = ct['C_j_3'] Ci4 = ct['C_j_4'] Ci5 = ct['C_j_5'] policy = util.createPolicy(ct['policy']) #((ONE or THREE) and (TWO or FOUR)) print('access policy of this file :', policy) try: pruned = util.prune(policy, S) #[ONE, TWO] except Exception,e: raise Exception('your attributes do not satisfy the policy!') wi = util.getCoefficients(policy) #return {u'TWO': <pairing.Element>, u'FOUR': <pairing.Element>, u'THREE': <pairing.Element>, u'ONE': <pairing.Element>} eC0K0 = pair(C0,K0) ECi4wi = 0 for each in pruned: i = each.getAttribute() ECi4wi += Ci4[i] * wi[i] ewECi4wiK1 = pair(w ** ECi4wi,K1) PIeCi1K1eCi2uCi5Kj2eCi3Kj3wi = 1 for each in pruned: j = each.getAttributeAndIndex() i = each.getAttribute() eCi1K1 = pair(Ci1[i],K1) eCi2uCi5Kj2 = pair(Ci2[i] * (u ** -Ci5[i]), Kj2[j]) eCi3Kj3 = pair(Ci3[i],Kj3[j]) PIeCi1K1eCi2uCi5Kj2eCi3Kj3wi *= (eCi1K1 * eCi2uCi5Kj2 * eCi3Kj3) ** wi[i] key_AES = (eC0K0/(ewECi4wiK1 * PIeCi1K1eCi2uCi5Kj2eCi3Kj3wi)) ** sk