【问题标题】:How to serialize/store the ciphertext encrypted by hybrid CPabe_BSW07 in Charm如何在Charm中序列化/存储混合CPabe_BSW07加密的密文
【发布时间】:2017-09-01 06:36:22
【问题描述】:

我想将混合cpabe_BSW07加密的密文存储在文件中,但是在pickling密文时发现错误:

引发 TypeError,“无法腌制 %s 个对象”% base.__name__ TypeError:无法腌制元素对象
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()

【问题讨论】:

    标签: python encryption encoding pickle charm-crypto


    【解决方案1】:

    哈哈哈,我现在知道怎么解决了:

    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()
    

    【讨论】:

    • 除了发布您的代码之外,还需要描述您是如何解决的以及问题所在。请edit你的回答让它变得更好,你可能会得到一个支持。
    【解决方案2】:

    我在尝试使用 json、pickle、simplejson 序列化 Charm crypto 生成的 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多