def encrypt_query(self, query: bytes): """ Encrypt the query to send to the server query - The query """ # Generate the ephemeral key ephemeral_key = generate_key() # Generate the symetric key print("Ephemeral: ", type(ephemeral_key)) print("Server PK: ", type(self.server_pk)) aes_key = encapsulate(ephemeral_key, self.server_pk) # Encrypt the query (this adds the nonce) cipher_text = aes_encrypt(aes_key, query) # Return the message and the symetric key return ephemeral_key.public_key.format(False) + cipher_text, aes_key
def encrypt(receiver_pubhex: str, msg: bytes) -> bytes: """ Encrypt with eth public key Parameters ---------- receiver_pubhex: str Receiver's ethereum public key hex string msg: bytes Data to encrypt Returns ------- bytes Encrypted data """ disposable_key = generate_key() receiver_pubkey = hex2pub(receiver_pubhex) aes_key = derive(disposable_key, receiver_pubkey) cipher_text = aes_encrypt(aes_key, msg) return disposable_key.public_key.format(False) + cipher_text
def test_aes(self): data = self.big_data key = os.urandom(16) self.assertEqual(data, aes_decrypt(key, aes_encrypt(key, data)))
def encrypt_answer(self, answer: bytes, aes_key: bytes): return aes_encrypt(aes_key, answer)