繁体   English   中英

如何编码数据以用于Charm基于属性的加密?

[英]How to encode data for use in Charm's attribute based encryption?

我正在使用Github的Charm Crypto。 我想使用基于属性的加密算法。 测试代码可以正常工作,但是它使用从PairingGroup生成的随机消息。 如何使用自己的数据进行加密?

>>> group = PairingGroup('SS512', secparam=512)
>>> msg = group.random(GT)

PairingGroup具有编码/解码方法,但未实现。 我只想尝试“ Hello world!”。

在charm / charm / test / toolbox / symcrypto_test.py下查看此类

class SymmetricCryptoAbstractionTest(unittest.TestCase):

def testAESCBC(self):
    self.MsgtestAESCBC(b"hello world")

def testAESCBCLong(self):
    self.MsgtestAESCBC(b"Lots of people working in cryptography have no deep \
   concern with real application issues. They are trying to discover things \
    clever enough to write papers about -- Whitfield Diffie.")

def testAESCBC_Seperate(self):
    self.MsgTestAESCBCSeperate(b"Lots of people working in cryptography have no deep \
    concern with real application issues. They are trying to discover things \
    clever enough to write papers about -- Whitfield Diffie.")

def MsgtestAESCBC(self,msg):
    groupObj = PairingGroup('SS512')
    a =  SymmetricCryptoAbstraction(sha1(groupObj.random(GT)))
    ct = a.encrypt(msg)
    dmsg = a.decrypt(ct);
    assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)

def MsgTestAESCBCSeperate(self,msg):
    groupObj = PairingGroup('SS512')
    ran = groupObj.random(GT)
    a =  SymmetricCryptoAbstraction(sha1(ran))
    ct = a.encrypt(msg)        
    b =  SymmetricCryptoAbstraction(sha1(ran))
    dmsg = b.decrypt(ct);
    assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
from charm.toolbox.pairinggroup import PairingGroup, GT
from ac17 import AC17CPABE


def main():
# instantiate a bilinear pairing map
pairing_group = PairingGroup('MNT224')

# AC17 CP-ABE under DLIN (2-linear)
cpabe = AC17CPABE(pairing_group, 2)

# run the set up
(pk, msk) = cpabe.setup()

# generate a key
attr_list = ['ONE', 'TWO', 'THREE']
key = cpabe.keygen(pk, msk, attr_list)

# choose a random message
msg = pairing_group.random(GT)

# generate a ciphertext
policy_str = '((ONE and THREE) and (TWO OR FOUR))'
ctxt = cpabe.encrypt(pk, msg, policy_str)

# decryption
rec_msg = cpabe.decrypt(pk, ctxt, key)
if debug:
    if rec_msg == msg:
        print ("Successful decryption.")
    else:
        print ("Decryption failed.")

if __name__ == "__main__":
    debug = False
    main()

我也想加密“ hello world”,如何将类型为str或字节的msg转换为pairing.Element?

暂无
暂无

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

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