您好,我正在尝试实现一个程序,该程序将生成包含银行交易的 csv 文件,并计算总和,然后它还将使用同态加密来计算它,但我在进行同态加密时的输出始终是加密的。 这是代码:
import tkinter as tk
from tkinter import filedialog
import csv
import random
import time
from lightphe import LightPHE
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]))
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)
total_sum_str = str(total_sum_decrypted) # Represent the decrypted sum as a string
end_time = time.time()
result_text = f"Total amount (homomorphic encryption): {total_sum_str}\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))
generate_and_write_transactions("transaction_history.csv", 10)
root = tk.Tk()
root.title("Bank Account testing")
root.geometry("400x300")
compute_button = tk.Button(root, text="Compute Total Amount (Traditional)", command=compute_total_amount)
compute_button.pack(pady=5)
compute_homomorphic_button = tk.Button(root, text="Compute Total Amount (Homomorphic)", command=compute_total_amount_homomorphic)
compute_homomorphic_button.pack(pady=5)
result_label = tk.Label(root, text="")
result_label.pack()
root.mainloop()
浮点数在 LightPHE 中不是无效的。它们被转换为该模块的整数。这样,我们就能以恒定的百分比增加密文。
from lightphe import LightPHE
cs = LightPHE(algorithm_name = "Paillier")
m1 = 17
c1 = cs.encrypt(m1)
# increasing something 5%
k = 1.05
# scalar multiplication - private key is not required!
c4 = k * c1
# proof of work
assert cs.decrypt(c4) == k * m1
此外,如果您将明文设置为张量,那么您就可以恢复该浮点数。您可以使用这种方法跳过整数转换。
from lightphe import LightPHE
cs = LightPHE(algorithm_name = "Paillier")
m = 1.5
c = cs.encrypt(plaintext = [m])
p = cs.decrypt(c)
assert p[0] == m