Hello I have the following code with which I am trying to test homomorphic encryption using different algorithms for a csv file generated random but the Exponential ElGamal approach does not work properly my program stops and gives not responding even for a small number of transactions generated
The code is here:
import tkinter as tk
from tkinter import filedialog, scrolledtext, ttk
import csv
import random
import time
from lightphe import LightPHE
import os
def generate_and_write_transactions(filename, num_transactions):
transactions = []
for _ in range(num_transactions):
amount_received = round(random.uniform(10, 500), 2)
transactions.append(("Deposit", amount_received))
with open(filename, "w", newline="") as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(["Transaction Type", "Amount"])
csvwriter.writerows(transactions)
def compute_total_amount():
try:
start_time = time.time()
filename = "transaction_history.csv"
total_amount = 0
with open(filename, 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header row
for row in reader:
if row[0] == 'Deposit':
total_amount += float(row[1])
end_time = time.time()
result_label.config(text=f"Total amount (traditional computation): ${total_amount:.2f}nTime taken: {end_time - start_time:.6f} seconds")
except Exception as e:
result_label.config(text="Error: " + str(e))
def compute_total_amount_homomorphic():
try:
start_time = time.time()
filename = "transaction_history.csv"
cs = LightPHE(algorithm_name="Paillier")
encrypted_values = []
with open(filename, 'r') as file:
reader = csv.reader(file)
next(reader)
for row in reader:
if row[0] == 'Deposit':
encrypted_amount = cs.encrypt(plaintext=[float(row[1])]) # Encrypt float as a tensor
encrypted_values.append(encrypted_amount)
# Homomorphically add all encrypted values together
total_encrypted_sum = encrypted_values[0]
for encrypted_amount in encrypted_values[1:]:
total_encrypted_sum += encrypted_amount
# Decrypt the total sum
total_sum_decrypted = cs.decrypt(total_encrypted_sum)[0] # Convert tensor back to float
end_time = time.time()
result_text = f"Total amount (homomorphic encryption Paillier): ${total_sum_decrypted:.2f}nTime taken: {end_time - start_time:.6f} seconds"
result_label.config(text=result_text)
except Exception as e:
result_label.config(text="Error: " + str(e))
def compute_total_amount_elgamal():
try:
start_time = time.time()
filename = "transaction_history.csv"
cs = LightPHE(algorithm_name="Exponential-ElGamal")
encrypted_values = []
with open(filename, 'r') as file:
reader = csv.reader(file)
next(reader)
for row in reader:
if row[0] == 'Deposit':
encrypted_amount = cs.encrypt(plaintext=[float(row[1])]) # Encrypt float as a tensor
encrypted_values.append(encrypted_amount)
# Homomorphically add all encrypted values together
total_encrypted_sum = encrypted_values[0]
for encrypted_amount in encrypted_values[1:]:
total_encrypted_sum += encrypted_amount
# Decrypt the total sum
total_sum_decrypted = cs.decrypt(total_encrypted_sum)[0] # Convert tensor back to float
end_time = time.time()
result_text = f"Total amount (homomorphic encryption ElGamal): ${total_sum_decrypted:.2f}nTime taken: {end_time - start_time:.6f} seconds"
result_label.config(text=result_text)
except Exception as e:
result_label.config(text="Error: " + str(e))
def update_transaction_count():
try:
global num_transactions
num_transactions = int(transaction_entry.get())
generate_and_write_transactions("transaction_history.csv", num_transactions)
transaction_count_label.config(text=f"Transactions generated: {num_transactions}")
except ValueError:
transaction_count_label.config(text="Invalid input. Please enter a valid integer.")
def view_csv():
try:
with open("transaction_history.csv", 'r') as file:
data = file.read()
csv_display.delete(1.0, tk.END)
csv_display.insert(tk.END, data)
except FileNotFoundError:
csv_display.delete(1.0, tk.END)
csv_display.insert(tk.END, "CSV file not found.")
def delete_csv_file():
try:
os.remove("transaction_history.csv")
root.destroy()
except FileNotFoundError:
root.destroy()
root = tk.Tk()
root.title("Bank Account Testing")
root.geometry("800x600")
# Styling
root.configure(bg="#8f9c92")
style = ttk.Style()
style.configure('TButton', background='#3a32a8', foreground='#070807')
# Input for number of transactions
transaction_label = tk.Label(root, text="Enter number of transactions to generate:", bg="#f0f0f0")
transaction_label.pack(pady=(20, 0))
transaction_entry = tk.Entry(root, width=10)
transaction_entry.pack()
generate_button = ttk.Button(root, text="Generate Transactions", command=update_transaction_count)
generate_button.pack(pady=10)
transaction_count_label = tk.Label(root, text="", bg="#8f9c92")
transaction_count_label.pack()
# Display CSV data
csv_display = scrolledtext.ScrolledText(root, width=60, height=15, wrap=tk.WORD)
csv_display.pack(pady=10)
view_csv_button = ttk.Button(root, text="View CSV", command=view_csv)
view_csv_button.pack(pady=5)
# Buttons to compute total amount
compute_button = ttk.Button(root, text="Compute Total Amount (Traditional)", command=compute_total_amount)
compute_button.pack(pady=10)
compute_homomorphic_button = ttk.Button(root, text="Compute Total Amount (Homomorphic encryption Paillier)", command=compute_total_amount_homomorphic)
compute_homomorphic_button.pack(pady=5)
# Button to compute total amount using ElGamal encryption
compute_elgamal_button = ttk.Button(root, text="Compute Total Amount (Homomorphic encryption ElGamal)", command=compute_total_amount_elgamal)
compute_elgamal_button.pack(pady=5)
result_label = tk.Label(root, text="", bg="#8f9c92")
result_label.pack()
# Exit button to delete CSV file and close the application
exit_button = ttk.Button(root, text="Exit", command=delete_csv_file)
exit_button.pack(pady=10)
root.mainloop()
The program works well for Paillier and normal addition but It crashes when trying to use it for exponential elgamal algorithm