private static void checkSatisfy(BswabePolicy p, BswabePrv prv) { int i, l; String prvAttr; p.satisfiable = false; if (p.children == null || p.children.length == 0) { for (i = 0; i < prv.comps.size(); i++) { prvAttr = prv.comps.get(i).attr; // System.out.println("prvAtt:" + prvAttr); // System.out.println("p.attr" + p.attr); if (prvAttr.compareTo(p.attr) == 0) { // System.out.println("=staisfy="); p.satisfiable = true; p.attri = i; break; } } } else { for (i = 0; i < p.children.length; i++) checkSatisfy(p.children[i], prv); l = 0; for (i = 0; i < p.children.length; i++) if (p.children[i].satisfiable) l++; if (l >= p.k) p.satisfiable = true; } }
private static void decNodeFlatten(Element r, Element exp, BswabePolicy p, BswabePrv prv, BswabePub pub) { if (p.children == null || p.children.length == 0) decLeafFlatten(r, exp, p, prv, pub); else decInternalFlatten(r, exp, p, prv, pub); }
private static void decInternalFlatten(Element r, Element exp, BswabePolicy p, BswabePrv prv, BswabePub pub) { int i; Element t, expnew; t = pub.p.getZr().newElement(); expnew = pub.p.getZr().newElement(); for (i = 0; i < p.satl.size(); i++) { lagrangeCoef(t, p.satl, (p.satl.get(i)).intValue()); expnew = exp.duplicate(); expnew.mul(t); decNodeFlatten(r, expnew, p.children[p.satl.get(i) - 1], prv, pub); } }
Bswabe.setup(pub, msk); println("//demo for co.junwei.bswabe: end to setup"); prv = Bswabe.keygen(pub, msk, attr); println("//demo for co.junwei.bswabe: end to keygen"); prv_delegate_ok = Bswabe.delegate(pub, prv, attr_delegate_ok); println("//demo for co.junwei.bswabe: end to delegate_ok"); prv_delegate_ko = Bswabe.delegate(pub, prv, attr_delegate_ko); println("//demo for co.junwei.bswabe: end to delegate_ko"); BswabeCphKey crypted = Bswabe.enc(pub, policy); cph = crypted.cph; println("//demo for co.junwei.bswabe: end to enc"); result = Bswabe.dec(pub, prv, cph); println("//demo for co.junwei.bswabe: end to dec"); if ((result.b == true) && (result.e.equals(crypted.key) == true)) result = Bswabe.dec(pub, prv_delegate_ok, cph); println("//demo for co.junwei.bswabe: end to dec with ok delegated key"); if ((result.b == true) && (result.e.equals(crypted.key) == true)) result = Bswabe.dec(pub, prv, cph); println("//demo for co.junwei.bswabe: end to dec"); if ((result.b == true) && (result.e.equals(crypted.key) == true)) result = Bswabe.dec(pub, prv_delegate_ko, cph);
public static BswabeElementBoolean dec(BswabePub pub, BswabePrv prv, BswabeCph cph) { Element t; Element m; BswabeElementBoolean beb = new BswabeElementBoolean(); m = pub.p.getGT().newElement(); t = pub.p.getGT().newElement(); checkSatisfy(cph.p, prv); if (!cph.p.satisfiable) { System.err .println("cannot decrypt, attributes in key do not satisfy policy"); beb.e = null; beb.b = false; return beb; } pickSatisfyMinLeaves(cph.p, prv); decFlatten(t, cph.p, prv, pub); m = cph.cs.duplicate(); m.mul(t); /* num_muls++; */ t = pub.p.pairing(cph.c, prv.d); t.invert(); m.mul(t); /* num_muls++; */ beb.e = m; beb.b = true; return beb; }
public void enc(String pubfile, String policy, String inputfile, String encfile) throws Exception { BswabePub pub; BswabeCph cph; BswabeCphKey keyCph; byte[] plt; byte[] cphBuf; byte[] aesBuf; byte[] pub_byte; Element m; /* get BswabePub from pubfile */ pub_byte = Common.suckFile(pubfile); pub = SerializeUtils.unserializeBswabePub(pub_byte); keyCph = Bswabe.enc(pub, policy); cph = keyCph.cph; m = keyCph.key; System.err.println("m = " + m.toString()); if (cph == null) { System.out.println("Error happed in enc"); System.exit(0); } cphBuf = SerializeUtils.bswabeCphSerialize(cph); /* read file to encrypted */ plt = Common.suckFile(inputfile); aesBuf = AESCoder.encrypt(m.toBytes(), plt); // PrintArr("element: ", m.toBytes()); Common.writeCpabeFile(encfile, cphBuf, aesBuf); }
public void dec(String pubfile, String prvfile, String encfile, String decfile) throws Exception { byte[] aesBuf, cphBuf; byte[] plt; byte[] prv_byte; byte[] pub_byte; byte[][] tmp; BswabeCph cph; BswabePrv prv; BswabePub pub; /* get BswabePub from pubfile */ pub_byte = Common.suckFile(pubfile); pub = SerializeUtils.unserializeBswabePub(pub_byte); /* read ciphertext */ tmp = Common.readCpabeFile(encfile); aesBuf = tmp[0]; cphBuf = tmp[1]; cph = SerializeUtils.bswabeCphUnserialize(pub, cphBuf); /* get BswabePrv form prvfile */ prv_byte = Common.suckFile(prvfile); prv = SerializeUtils.unserializeBswabePrv(pub, prv_byte); BswabeElementBoolean beb = Bswabe.dec(pub, prv, cph); System.err.println("e = " + beb.e.toString()); if (beb.b) { plt = AESCoder.decrypt(beb.e.toBytes(), aesBuf); Common.spitFile(decfile, plt); } else { System.exit(0); } }
/** * @param * @author Junwei Wang(wakemecn@gmail.com) */ public void setup(String pubfile, String mskfile) throws IOException, ClassNotFoundException { byte[] pub_byte, msk_byte; BswabePub pub = new BswabePub(); BswabeMsk msk = new BswabeMsk(); Bswabe.setup(pub, msk); /* store BswabePub into mskfile */ pub_byte = SerializeUtils.serializeBswabePub(pub); Common.spitFile(pubfile, pub_byte); /* store BswabeMsk into mskfile */ msk_byte = SerializeUtils.serializeBswabeMsk(msk); Common.spitFile(mskfile, msk_byte); }
public void keygen(String pubfile, String prvfile, String mskfile, String attr_str) throws NoSuchAlgorithmException, IOException { BswabePub pub; BswabeMsk msk; byte[] pub_byte, msk_byte, prv_byte; /* get BswabePub from pubfile */ pub_byte = Common.suckFile(pubfile); pub = SerializeUtils.unserializeBswabePub(pub_byte); /* get BswabeMsk from mskfile */ msk_byte = Common.suckFile(mskfile); msk = SerializeUtils.unserializeBswabeMsk(pub, msk_byte); String[] attr_arr = LangPolicy.parseAttribute(attr_str); BswabePrv prv = Bswabe.keygen(pub, msk, attr_arr); /* store BswabePrv into prvfile */ prv_byte = SerializeUtils.serializeBswabePrv(prv); Common.spitFile(prvfile, prv_byte); }
private static void decFlatten(Element r, BswabePolicy p, BswabePrv prv, BswabePub pub) { Element one; one = pub.p.getZr().newElement(); one.setToOne(); r.setToOne(); decNodeFlatten(r, one, p, prv, pub); }