本文介绍了如何在Charm中序列化/存储由混合CPabe_BSW07加密的密文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to store the ciphertext encrypted by hybrid cpabe_BSW07 in files, but I found errors when pickling the ciphertext:


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

这篇关于如何在Charm中序列化/存储由混合CPabe_BSW07加密的密文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:39