def data_exchange()

in codelabs/trusted_space_codelab/src/client/sample_inference_client.py [0:0]


def data_exchange():
  text = input("Enter your prompt: ")
  data_key = Fernet.generate_key()
  f = Fernet(data_key)
  encrypted_message = f.encrypt(bytes(text, "utf-8"))
  wrapped_dek_response = kms_client.encrypt(request={"name": key_name, "plaintext": data_key})
  wrapped_dek = base64.b64encode(wrapped_dek_response.ciphertext).decode("utf-8")
  ciphertext = base64.b64encode(encrypted_message).decode("utf-8")
  payload = {
      "ciphertext": ciphertext,
      "wrapped_dek": wrapped_dek,
  }
  print("sending encrypted payload: ", payload)
  response = requests.post(os.environ["INFERENCE_SERVER_URL"], json=payload)
  data = response.json()
  print("received encrypted response", data)
  ciphertext = base64.b64decode(data["generated_code_ciphertext"])
  decrypted_message = f.decrypt(ciphertext)
  print("decrypted response: ", decrypted_message)