Spaces:
Running
Running
import gradio as gr | |
from phe import paillier | |
# Generate Paillier keypair | |
public_key, private_key = paillier.generate_paillier_keypair(n_length=1024) | |
def secure_encoding(input_data): | |
# print intro | |
# Print raw input_data | |
print("Raw Input Data:", input_data) | |
# Convert input data to a list | |
data = input_data.split(',') | |
print("Processed Input Data:", data) | |
# Encode data | |
encoded_data = [1 if val == "smoker" else 0 for val in data] | |
print("Encoded Data:", encoded_data) | |
# Encrypt and send to cloud | |
print("Encrypting data and send to cloud...") | |
print("===================================") | |
encrypted_data = [public_key.encrypt(x) for x in encoded_data] | |
print("Third party only sees encrypted data and cannot figure out the nature of the data or encoding") | |
print("=========================================") | |
print("Encrypted Ciphertext based on paillier looks like this -> :", encrypted_data[0].ciphertext()) | |
# Perform homomorphic addition to count smokers | |
encrypted_count = encrypted_data[0] | |
for i in range(1, len(encrypted_data)): | |
encrypted_count += encrypted_data[i] | |
# Output encrypted count | |
print("Result is also encrypted") | |
print("========================") | |
print("Encrypted Count:", encrypted_count) | |
# Send result to the data owner | |
decrypted_count = private_key.decrypt(encrypted_count) | |
print("Send result to the data owner") | |
print("Decrypted Count:", decrypted_count) | |
return ( | |
f"Raw data (only data owner has access to the plain text): {data}", | |
f"Encrypted Ciphertext: {encrypted_data[0].ciphertext()}", | |
f"Run encrypted private query processing and generate encrypted result: {encrypted_count}", | |
f"Decrypted Result: {decrypted_count}", | |
"Data processing is complete. The encrypted count have been generated." | |
) | |
# Gradio Interface | |
iface = gr.Interface( | |
fn=secure_encoding, | |
inputs=gr.Textbox("smoker,non-smoker,smoker,smoker", placeholder="Enter data (e.g., smoker,non-smoker)"), | |
outputs=[ | |
gr.Textbox("text", label="Raw data"), | |
gr.Textbox("text", label="Encrypted data -- using private key"), | |
gr.Textbox("text", label="Encrypted Result -- not readable by the third party without the private key"), | |
gr.Textbox("text", label="Decrypted Result -- total numbers of smokers in this DB") | |
], | |
title="Privacy Preserving Encrypted Query Demonstration", | |
description=""" | |
This demonstration showcases a method for analyzing sensitive data while protecting individual privacy. It uses Paillier encryption (partial homomorphic) and , a powerful technique that allows calculations on encrypted data without revealing the actual values. | |
## Welcome to DNAsafe demo! | |
### Why is this important? | |
- Privacy Protection: Analyzing personal data often raises privacy concerns. This method keeps individual data private even while performing calculations, like counting the number of "smokers" in a dataset. | |
- Secure Collaboration: Sensitive data can be analyzed jointly by multiple parties without anyone directly accessing the raw data and the result. Only authorized stakeholders and data owner have access to the private key that allows them to see the raw text. | |
- Cloud Computing: This approach enables secure analysis on cloud platforms, leveraging their computing power without jeopardizing data privacy. | |
Watch the demo to see: | |
* How Paillier encryption works to conceal data values. | |
* How encrypted data can be processed and analyzed (e.g., counting smokers). | |
* How the final results are obtained while maintaining privacy. | |
Press the submit button with the suggested values here. | |
""", | |
) | |
# Display the image and then launch the Gradio app | |
iface.launch() | |