def encrypt(): print('entered'); input_client_public_key = request.json.get('pub_client') input_inviou_public_key = request.json.get('pub_inviou') intputArray = request.json.get('input') if not intputArray: return jsonify({'Error': 'Missing required input data to encrypt input = [{id:1,plaintext:123},{id:2,plaintext:222}....]' }) if not input_client_public_key: return jsonify({'Error': 'Missing required public key'}) if input_inviou_public_key: inviou_public_key = keys.UmbralPublicKey.from_bytes(input_inviou_public_key) client_public_key = keys.UmbralPublicKey.from_bytes(input_client_public_key) results_inviou = [] results_client = [] for item in intputArray: input_plaintext = item.get('plaintext'); # public_key = private_key.get_pubkey() ciphertext, umbral_capsule = umbral.encrypt(client_public_key, input_plaintext.encode()) ciphertext_encoded = base64.b64encode(ciphertext).decode("utf-8") umbral_capsule_encoded = base64.b64encode(umbral_capsule.to_bytes()).decode("utf-8") results_client.append({'id':item.get('id'),'ciphertext': ciphertext_encoded, 'capsule': umbral_capsule_encoded}) if input_inviou_public_key: ciphertext_inviou, umbral_capsule_inviou = umbral.encrypt(inviou_public_key, input_plaintext.encode()) ciphertext_encoded_inviou = base64.b64encode(ciphertext_inviou).decode("utf-8") umbral_capsule_encoded_inviou = base64.b64encode(umbral_capsule_inviou.to_bytes()).decode("utf-8") results_inviou.append({'id':item.get('id'),'ciphertext': ciphertext_encoded_inviou, 'capsule': umbral_capsule_encoded_inviou}) # print('ciphertext_encoded:', ciphertext_encoded) # print('umbral_capsule_encoded:', umbral_capsule_encoded) # return jsonify({'ciphertext': ciphertext_encoded, 'capsule': umbral_capsule_encoded}) results = results['encrypted'] = results_client; # results.append({'encrypted':results_client}); if input_inviou_public_key: results['encrypted_inviou'] = results_inviou; # results.append({'encrypted_inviou':results_inviou}); return jsonify(results);
def test_alice_sends_fake_kfrag_to_ursula(N, M): priv_key_alice = keys.UmbralPrivateKey.gen_key() pub_key_alice = priv_key_alice.get_pubkey() priv_key_bob = keys.UmbralPrivateKey.gen_key() pub_key_bob = priv_key_bob.get_pubkey() plaintext = b'attack at dawn' ciphertext, capsule = umbral.encrypt(pub_key_alice, plaintext) cleartext = umbral.decrypt(capsule, priv_key_alice, ciphertext) assert cleartext == plaintext k_frags, vkeys = umbral.split_rekey(priv_key_alice, pub_key_bob, M, N) # Alice tries to frame the first Ursula by sending her a random kFrag k_frags[0].bn_key = BigNum.gen_rand() for k_frag in k_frags: c_frag = umbral.reencrypt(k_frag, capsule) capsule.attach_cfrag(c_frag) with pytest.raises(Exception): _ = umbral.decrypt(capsule, priv_key_bob, ciphertext, pub_key_alice)
def encrypt_financial_record(pub_key, record_content): """ encrypt financial record XBRL content :param pub_key: public key of the orignial financial Record Originator, to be used for encrypting :param record_content: XBRL content of the financial record :return: a tuple holding the cipher text and capsul info """ ciphertext, umbral_capsule = umbral.encrypt(pub_key, record_content) return ciphertext, umbral_capsule
def test_simple_api(N, M, curve=default_curve()): """Manually injects umbralparameters for multi-curve testing.""" params = UmbralParameters(curve=curve) priv_key_alice = keys.UmbralPrivateKey.gen_key(params=params) pub_key_alice = priv_key_alice.get_pubkey() priv_key_bob = keys.UmbralPrivateKey.gen_key(params=params) pub_key_bob = priv_key_bob.get_pubkey() plain_data = b'attack at dawn' ciphertext, capsule = umbral.encrypt(pub_key_alice, plain_data) cleartext = umbral.decrypt(capsule, priv_key_alice, ciphertext) assert cleartext == plain_data rekeys, _unused_vkeys = umbral.split_rekey(priv_key_alice, pub_key_bob, M, N, params=params) for rekey in rekeys: c_frag = umbral.reencrypt(rekey, capsule, params=params) capsule.attach_cfrag(c_frag) reenc_cleartext = umbral.decrypt(capsule, priv_key_bob, ciphertext, pub_key_alice) assert reenc_cleartext == plain_data
#2 # Generate keys for Alice and Bob alice_priv_key = keys.UmbralPrivateKey.from_bytes("effIKT60Ei8M9EtLGb36Gt6+ZjXn7uj8okftqEjlKCE=") alice_pub_key = alice_priv_key.get_pubkey() # print("Alice priv: ", alice_priv_key.to_bytes().hex()) # print("Alice pub: " , alice_pub_key.to_bytes().hex()) bob_priv_key = keys.UmbralPrivateKey.gen_key() bob_pub_key = bob_priv_key.get_pubkey() #3 # Encrypt some data for Alice plaintext = b'good aaaaaaaapppppppppppppppppppppppppppppppppppppppaaaaaaaa and bad and stuff like this i dont know if we should or shouldnt' alice_ciphertext, umbral_capsule = umbral.encrypt(alice_pub_key, plaintext) new_plainText = b'shit' alice_new_ciphertext, new_umbral_capsule = umbral.encrypt(alice_pub_key, plaintext) print("Alice cyppher: ", alice_ciphertext) print("Plain Text Hex Size: ", len(plaintext)) print("Cypher Text Size: ",len(alice_ciphertext.hex())) #4 # Decrypt data for Alice alice_decrypted_data = umbral.decrypt(umbral_capsule, alice_priv_key, alice_ciphertext, alice_pub_key) print(alice_decrypted_data) #5 # Bob receives a capsule through a side channel (s3, ipfs, Google cloud, etc) bob_capsule = umbral_capsule
def test_pub_key_encryption(alices_keys): priv_key_alice, pub_key_alice = alices_keys plain_data = b'attack at dawn' ciphertext, capsule = umbral.encrypt(pub_key_alice, plain_data) cleartext = umbral.decrypt(capsule, priv_key_alice, ciphertext) assert cleartext == plain_data