简体   繁体   English

Charm中如何序列化/存储混合CPabe_BSW07加密的密文

[英]How to serialize/store the ciphertext encrypted by hybrid CPabe_BSW07 in Charm

I want to store the ciphertext encrypted by hybrid cpabe_BSW07 in files, but I found errors when pickling the ciphertext:我想将混合cpabe_BSW07加密的密文存储在文件中,但是在pickle密文时发现错误:

raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle Element objects
from charm.toolbox.pairinggroup import PairingGroup
from charm.schemes.abenc.abenc_bsw07 import CPabe_BSW07
from 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) and (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, I know how to solve now:哈哈哈,我现在知道怎么解决了:

from charm.toolbox.pairinggroup import PairingGroup
from charm.schemes.abenc.abenc_bsw07 import CPabe_BSW07
from 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) and (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"])
  for 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"])
  for 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"])
  for 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"])
  for 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()

I had the same problem trying to serialize a dict generated by Charm crypto using json, pickle, simplejson and solved it by using charm.core.engine.util .objectToBytes|bytesToObject methods.我在尝试使用 json、pickle、simplejson 序列化由 Charm 加密生成的 dict 时遇到了同样的问题,并通过使用Charm.core.engine.util .objectToBytes|bytesToObject 方法解决了这个问题。

from charm.core.engine.util import objectToBytes, bytesToObject
<...>
emsg = objectToBytes(cipher_text,group)
msg = bytesToObject(fin.read(),group)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM