def cpabe_dec(): print("Testing Python bindings for PyOpenABE...") openabe = pyopenabe.PyOpenABE() cpabe = openabe.CreateABEContext("CP-ABE") usr_id = "bob" mpk = b'0' uk = b'0' with open("./mpk.txt", "rb") as f: mpk = f.read() f.close() cpabe.importPublicParams(mpk) with open("./bob_key.txt", "rb") as f: uk = f.read() f.close() cpabe.importUserKey(usr_id, uk) with open("./alice_ct.txt", "rb") as f: ct = f.read() f.close() time_dec = time.time() pt2 = cpabe.decrypt(usr_id, ct) print("dec_time:", time.time() - time_dec) print("bob dec result:", pt2) print("CP-ABE dec!")
def cpabe_usrkey( ): # TA operate; not import msk; flag to check whether msk has existed. print("Testing Python bindings for PyOpenABE...") openabe = pyopenabe.PyOpenABE() cpabe = openabe.CreateABEContext("CP-ABE") with open("./mpk.txt", 'rb') as f: mpk_load = f.read() f.close() with open("./msk.txt", 'rb') as f: msk_load = f.read() f.close() cpabe.importSecretParams(msk_load) cpabe.importPublicParams(mpk_load) #cpabe.keygen("|two|three|", "alice") usr_id = "bob" usr_attri = "Dept:SecurityResearch|level = 2| Company:ByteDance|Sex:female" cpabe.keygen(usr_attri, usr_id) uk = cpabe.exportUserKey(usr_id) with open("./keys/sk.txt", 'wb') as f: f.write(uk) f.close() print("CP-ABE userkey gen end!")
def cpabe_usrkey( usr_attri ): # TA operate; not import msk; flag to check whether msk has existed. print("TA generate sk for users.") openabe = pyopenabe.PyOpenABE() cpabe = openabe.CreateABEContext("CP-ABE") with open("./mpk.txt", 'rb') as f: mpk_load = f.read() f.close() with open("./msk.txt", 'rb') as f: msk_load = f.read() f.close() cpabe.importSecretParams(msk_load) cpabe.importPublicParams(mpk_load) #cpabe.keygen("|two|three|", "alice") usr_id = "bob" # usr_attri = "Dept:SecurityResearch|level = 2|Company:ByteDance|Sex:female" print("input attri", usr_attri) cpabe.keygen(usr_attri, usr_id) uk = cpabe.exportUserKey(usr_id) # should write to db; filepath = current_app.config['ALBUMY_UPLOAD_PATH'] + "/sk.txt" with open(filepath, 'wb') as f: f.write(uk) f.close() print("CP-ABE userkey gen end!") return 0
def cpabe_enc_cli(message, policy): # message: bytes; # policy : str; print("Deal client abe enc request") openabe = pyopenabe.PyOpenABE() cpabe = openabe.CreateABEContext("CP-ABE") with open("./keys/mpk.txt", "rb") as f: mpk = f.read() f.close() cpabe.importPublicParams(mpk) # pt1 = b"hello world!" # ct = cpabe.encrypt("((one or two) and three)", pt1) time_enc = time.time() # file_policy = "(((Dept:SecurityResearch) or (level >= 4 )) and (Company:ByteDance)) and date < April 18, 2021" ct = cpabe.encrypt(policy, message) print("enc time:", time.time() - time_enc) ''' with open("./alice_ct.txt", "wb") as f: f.write(ct) f.close() ''' print("CP-ABE enc end.") return ct
def share(): public_key = get_public_key() secret_key = get_secret_key() try: json = request.get_json() attributes = check_attributes(json['attributes']) except (KeyError, AttributeError, ValueError) as ex: return abort(400, str(ex)) openabe = pyopenabe.PyOpenABE() context = openabe.CreateABEContext('KP-ABE') context.importPublicParams(public_key) context.importSecretParams(secret_key) key_name = ''.join(random.choices(string.ascii_uppercase, k=10)) context.keygen(' AND '.join(attributes), key_name) return jsonify({ 'sharekey': { 'public': public_key, 'share': context.exportUserKey(key_name).decode('US-ASCII') }, })
def _do_decrypt(abe, ciphertext, public_key, secret_key, encoding = 'US-ASCII'): openabe = pyopenabe.PyOpenABE() context = openabe.CreateABEContext('KP-ABE') context.importPublicParams(public_key) key_name = ''.join(random.choices(string.ascii_uppercase, k=10)) context.importUserKey(key_name, secret_key) return context.decrypt(key_name, ciphertext).decode(encoding)
def _do_share(abe, attributes): openabe = pyopenabe.PyOpenABE() context = openabe.CreateABEContext('KP-ABE') context.importPublicParams(abe.public_key) context.importSecretParams(abe.secret_key) key_name = ''.join(random.choices(string.ascii_uppercase, k=10)) context.keygen(attributes, key_name) return context.exportUserKey(key_name).decode('US-ASCII')
def _do_encrypt(abe, cleartext, attributes, encoding = 'US-ASCII'): # logging.debug(f'Encrypting with {attributes}') openabe = pyopenabe.PyOpenABE() context = openabe.CreateABEContext('KP-ABE') context.importPublicParams(abe.public_key) context.importSecretParams(abe.secret_key) ciphertext = context.encrypt(attributes, cleartext.encode(encoding)).decode('US-ASCII') # logging.debug(f'Ciphertext {ciphertext[0:20]}...{ciphertext[-20:]}') return ciphertext
def decrypt(): try: json = request.get_json() ciphertext = json['ciphertext'] except (KeyError, AttributeError, ValueError) as ex: return abort(400, str(ex)) openabe = pyopenabe.PyOpenABE() context = openabe.CreateABEContext('KP-ABE') key_name = ''.join(random.choices(string.ascii_uppercase, k=10)) context.importPublicParams(get_public_key()) context.importUserKey(key_name, get_share_key()) return jsonify({'cleartext': context.decrypt(key_name, ciphertext)})
def cpabe_init(): print("Testing Python bindings for PyOpenABE...") openabe = pyopenabe.PyOpenABE() cpabe = openabe.CreateABEContext("CP-ABE") cpabe.generateParams() msk = cpabe.exportSecretParams() mpk = cpabe.exportPublicParams() with open("./mpk.txt", "wb") as f: f.write(mpk) f.close() with open("./msk.txt", "wb") as f: f.write(msk) f.close() print("CP-ABE Init Success!")
def encrypt(): try: json = request.get_json() attributes = check_attributes(json['attributes']) text = json['text'] except (KeyError, AttributeError, ValueError) as ex: return abort(400, str(ex)) openabe = pyopenabe.PyOpenABE() context = openabe.CreateABEContext('KP-ABE') context.importPublicParams(get_public_key()) context.importSecretParams(get_secret_key()) return jsonify({ 'attributes': attributes, 'ciphertext': context.encrypt('|'.join(attributes), text.encode("UTF-8")) })
def cpabe_enc(): print("Testing Python bindings for PyOpenABE...") openabe = pyopenabe.PyOpenABE() cpabe = openabe.CreateABEContext("CP-ABE") with open("./mpk.txt", "rb") as f: mpk = f.read() f.close() cpabe.importPublicParams(mpk) pt1 = b"hello world!" # ct = cpabe.encrypt("((one or two) and three)", pt1) time_enc = time.time() file_policy = "(((Dept:SecurityResearch) or (level >= 4 )) and (Company:ByteDance)) and date < April 18, 2021" ct = cpabe.encrypt(file_policy, pt1) print("enc time:", time.time() - time_enc) with open("./alice_ct.txt", "wb") as f: f.write(ct) f.close() print("CP-ABE enc end.")
def cpabe_test(): print("Testing Python bindings for PyOpenABE...") openabe = pyopenabe.PyOpenABE() cpabe = openabe.CreateABEContext("CP-ABE") cpabe.generateParams() #cpabe.keygen("|two|three|", "alice") cpabe.keygen( "Dept:SecurityResearch|level = 2| Company:ByteDance|Sex:female", "alice") pt1 = b"hello world!" # ct = cpabe.encrypt("((one or two) and three)", pt1) time_enc = time.time() ct = cpabe.encrypt( "(((Dept:SecurityResearch) or (level >= 4 )) and (Company:ByteDance))", pt1) print("enc time:", time.time() - time_enc) print("ABE CT: ", len(ct)) time_dec = time.time() pt2 = cpabe.decrypt("alice", ct) print("dec_time:", time.time() - time_dec) print("PT: ", pt2) assert pt1 == pt2, "Didn't recover the message!" print("Testing key import") msk = cpabe.exportSecretParams() mpk = cpabe.exportPublicParams() print("type mpk", type(mpk), mpk) uk = cpabe.exportUserKey("alice") with open("./mpk.txt", 'wb') as f: f.write((mpk)) f.close() # print("alice sk:", uk) with open("./mpk.txt", 'rb') as f: mpk_load = f.read() f.close() # print("alice sk:", uk) cpabe2 = openabe.CreateABEContext("CP-ABE") print("mpl_load tyep:", type(mpk_load), type(mpk), len(mpk)) #temp = bytes(mpk_load, encoding='utf-8') #print("tyem ypt:", type(temp), len(temp)) cpabe2.importSecretParams(msk) cpabe2.importPublicParams(mpk_load) cpabe2.importUserKey("alice", uk) ct = cpabe2.encrypt( "(((Dept:SecurityResearch) or (level >= 4 )) and (Company:ByteDance))", pt1) print("ABE CT: ", len(ct)) pt2 = cpabe2.decrypt("alice", ct) print("PT: ", pt2) assert pt1 == pt2, "Didn't recover the message!" print("CP-ABE Success!")
from __future__ import print_function import pyopenabe print("Testing Python bindings for PyOpenABE...") openabe = pyopenabe.PyOpenABE() cpabe = openabe.CreateABEContext("CP-ABE") cpabe.generateParams() #cpabe.keygen("|two|three|", "alice") cpabe.keygen("Dept:SecurityResearch|level = 2| Company:ByteDance", "alice") pt1 = b"hello world!" # ct = cpabe.encrypt("((one or two) and three)", pt1) ct = cpabe.encrypt( "(((Dept:SecurityResearch) or (level >= 4 )) and (Company:ByteDance))", pt1) print("ABE CT: ", len(ct)) pt2 = cpabe.decrypt("alice", ct) print("PT: ", pt2) assert pt1 == pt2, "Didn't recover the message!" print("Testing key import") msk = cpabe.exportSecretParams() mpk = cpabe.exportPublicParams() uk = cpabe.exportUserKey("alice")