python - How to serialize/store the ciphertext encrypted by hybrid CPabe_BSW07 in Charm -
i want store ciphertext encrypted hybrid cpabe_bsw07 in files, found errors when pickling ciphertext:
raise typeerror, "can't pickle %s objects" % base.__name__ typeerror: can't pickle element objects
from charm.toolbox.pairinggroup import pairinggroup charm.schemes.abenc.abenc_bsw07 import cpabe_bsw07 charm.adapters.abenc_adapt_hybrid import hybridabenc import pickle if __name__ == "__main__": groupobj = pairinggroup('ss512') cpabe = cpabe_bsw07(groupobj) hyb_abe = hybridabenc(cpabe, groupobj) (pk, mk) = hyb_abe.setup() access_policy = '((four or three) , (two or one))' sk = hyb_abe.keygen(pk, mk, ['one', 'two', 'three']) sourcefile = open("source.dat", 'rb') plaintext = sourcefile.read() sourcefile.close() encryptedfile = open("encrypted.dat", 'wb') ciphertext = hyb_abe.encrypt(pk, plaintext, access_policy) pickle.dump(ciphertext, encryptedfile) encryptedfile.close()
hahaha, know how solve now:
from charm.toolbox.pairinggroup import pairinggroup charm.schemes.abenc.abenc_bsw07 import cpabe_bsw07 charm.adapters.abenc_adapt_hybrid import hybridabenc import pickle if __name__ == "__main__": groupobj = pairinggroup('ss512') cpabe = cpabe_bsw07(groupobj) hyb_abe = hybridabenc(cpabe, groupobj) (pk, mk) = hyb_abe.setup() access_policy = '((four or three) , (two or one))' sk = hyb_abe.keygen(pk, mk, ['one', 'two', 'three']) sourcefile = open("source.dat", 'rb') plaintext = sourcefile.read() sourcefile.close() encryptedfile = open("encrypted.dat", 'wb') ciphertext = hyb_abe.encrypt(pk, plaintext, access_policy) ciphertext["c1"]["c"] = groupobj.serialize(ciphertext["c1"]["c"]) key in ciphertext["c1"]["cy"] : ciphertext["c1"]["cy"][key] = groupobj.serialize(ciphertext["c1"]["cy"][key]) ciphertext["c1"]["c_tilde"] = groupobj.serialize(ciphertext["c1"]["c_tilde"]) key in ciphertext["c1"]["cyp"] : ciphertext["c1"]["cyp"][key] = groupobj.serialize(ciphertext["c1"]["cyp"][key]) pickle.dump(ciphertext, encryptedfile) encryptedfile.close() encryptedfile = open("encrypted.dat", 'rb') ciphertext2 = pickle.load(encryptedfile) ciphertext2["c1"]["c"] = groupobj.deserialize(ciphertext2["c1"]["c"]) key in ciphertext2["c1"]["cy"]: ciphertext2["c1"]["cy"][key] = groupobj.deserialize(ciphertext2["c1"]["cy"][key]) ciphertext2["c1"]["c_tilde"] = groupobj.deserialize(ciphertext2["c1"]["c_tilde"]) key in ciphertext2["c1"]["cyp"]: ciphertext2["c1"]["cyp"][key] = groupobj.deserialize(ciphertext2["c1"]["cyp"][key]) print hyb_abe.decrypt(pk, sk, ciphertext2) == plaintext encryptedfile.close()
Comments
Post a Comment