1

As far as I know the implemented Charm schemes only allow you to encrypt either randomly generated group element or msg string encoded as a group element. But these have limitations too, as the order of group elements are derived from prime numbers.

Is there a way to extend charm schemes to encrypt files?

Edited according to Artjom B's comment:

def encrypt(self, pk, M, object):
    key = self.group.random(GT)
    c1 = abenc.encrypt(pk, key, object)
    # instantiate a symmetric enc scheme from this key
    cipher = AuthenticatedCryptoAbstraction(sha1(key))
    c2 = cipher.encrypt(M)
    return { 'c1':c1, 'c2':c2 } 


f = open(pth,'r')
message = f.read()
pk, mk = abenc.setup() 
att_list=['TWO','FOUR']
access_policy = '((four or three) and (two or one))'  
sk = abenc.keygen(pk, mk, att_list)     
ct = encrypt(pk, message, access_policy)
  • This is probably not the place to be `guided through a bit detailed process` but a place for direct and concrete questions. Provide details about what you have tried and where you are failing. – Marcus Oct 13 '15 at 12:15
  • @marcus .. Thanks for clarification.. I have tried this, read the content of text file and encrypted that data using a key generated from symmetric encryption scheme. `key = self.group.random(GT) ` `c1 = abenc.encrypt(pk, key, object)` ` # instantiate a symmetric enc scheme from this key ` `cipher = AuthenticatedCryptoAbstraction(sha1(key)) ` `c2 = cipher.encrypt(M) ` But i need a way to encrypt an entire file irrespective of extension and should be able to decrypt it. – venkata praneeth Oct 13 '15 at 12:58

1 Answers1

2

When arbitrary data needs to be encrypted then hybrid encryption needs to be applied by encrypting a random element with the asymmetric cryptosystem and then deriving a symmetric key from the random element in order to encrypt the actual data with the resulting key.

Charm crypto provides a version of AES in order to encrypt arbitrary messages/data:

from charm.toolbox.pairinggroup import PairingGroup,ZR,G1,G2,GT,pair
from charm.toolbox.symcrypto import AuthenticatedCryptoAbstraction, SymmetricCryptoAbstraction
from charm.core.math.pairing import hashPair as extractor

group = PairingGroup("SS512")

r = group.random(G1)
msg = b"This is a secret message that is larger than the group elements and has to be encrypted symmetrically"

symcrypt = AuthenticatedCryptoAbstraction(extractor(r)) # or SymmetricCryptoAbstraction without authentication

# encryption
ciphertext = symcrypt.encrypt(msg)

# decryption
recoveredMsg = symcrypt.decrypt(ciphertext)

assert msg == recoveredMsg
print(recoveredMsg)

The extractor() function is actually a SHA-256 hash of the bytes of the element and thusly can handle all types of elements.

SymmetricCryptoAbstraction encrypts the data with AES in CBC mode with a random IV and PKCS#7 padding. It is internally Base64 encoded and but into a JSON string. This class is not a good fit for encrypting files, because the file data must be loaded fully into memory and may not work for big files. Use pyCrypto in order to encrypt files with a progressive encryption.

You will have to create your own file format in order to house the asymmetric ciphertext components and the symmetric ones beside each other.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222