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); } }
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); }