Search code examples
pythoncryptographycharm-crypto

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


I'm using Charm Crypto from Github. I'd like to use the attribute based encryption algorithms. The test code works fine, however, it uses a random message generated from PairingGroup. How do I use my own data for encryption?

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

The PairingGroup has encode/decode methods, but they are not implemented. I just want to try this with "Hello world!".


Solution

  • Look at this class under 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)