1+ """Flask application for Homomorphic Encryption-based Salary Prediction."""
2+
13import numpy as np
2- from flask import Flask , request , jsonify , render_template ,redirect ,url_for
4+ from flask import Flask , request , jsonify , render_template , redirect , url_for
35import json
6+ import os
47from cust import *
58from servercalc import *
69from os import path
710
811app = Flask (__name__ )
912
10- @app .route ('/' ,methods = ['GET' ,'POST' ])
11-
13+ @app .route ('/' , methods = ['GET' , 'POST' ])
1214def home ():
15+ """
16+ Render the home page.
1317
18+ Returns:
19+ Rendered index.html template.
20+ """
1421 return render_template ('index.html' )
1522
16- @app .route ('/customerEncryption' ,methods = ['GET' ,'POST' ])
23+ @app .route ('/customerEncryption' , methods = ['GET' , 'POST' ])
1724def customerEncryption ():
25+ """
26+ Handle customer encryption data submission.
27+
28+ - Generates encryption keys if not present.
29+ - Encrypts customer data and saves it to data.json.
30+ - Renders the cust.html template with encryption keys.
31+
32+ Returns:
33+ Rendered cust.html template with keys.
34+ """
35+ # Check if encryption keys exist; if not, generate and store them
1836 if not path .exists ('custkeys.json' ):
1937 storeKeys ()
2038
39+ # Retrieve existing encryption keys
2140 pub_key , priv_key = getKeys ()
2241
42+ # Extract features from the form and convert them to integers
2343 features = [int (x ) for x in request .form .values ()]
24- datafileCustomer = serializeDataCustomer (pub_key ,features )
44+ # Serialize and encrypt the customer data
45+ datafileCustomer = serializeDataCustomer (pub_key , features )
46+ # Save encrypted data to data.json
2547 with open ('data.json' , 'w' ) as file :
2648 json .dump (datafileCustomer , file )
2749
28- keys = [pub_key ,priv_key ]
29- return render_template ('cust.html' ,keys = keys )
50+ # Prepare keys to send to the frontend
51+ keys = {'public_key' : pub_key , 'private_key' : priv_key }
52+ # Render the customer details page with keys
53+ return render_template ('cust.html' , keys = keys )
3054
31- @app .route ('/company' ,methods = ['GET' ,'POST' ])
55+ @app .route ('/company' , methods = ['GET' , 'POST' ])
3256def company ():
33- datafileCompany = serializeDataCompany ()
57+ """
58+ Handle company data submission for salary prediction.
59+
60+ - Serializes and computes encrypted data.
61+ - Saves the result to answer.json.
62+ - Renders the company.html template with encrypted data.
63+
64+ Returns:
65+ Rendered company.html template with encrypted data.
66+ """
67+ # Serialize data for the company and compute encrypted results
68+ datafileCompany = serializeDataCompany ()
69+ # Save encrypted company data to answer.json
3470 with open ('answer.json' , 'w' ) as file :
35- json .dump (datafileCompany ,file )
36-
71+ json .dump (datafileCompany , file )
3772
38- return render_template ('company.html' ,datafileCompany = datafileCompany )
73+ # Render the company details page with encrypted data
74+ return render_template ('company.html' , datafileCompany = datafileCompany )
3975
40- #return redirect(url_for('result'))
76+ # Optionally redirect to the result page
77+ # return redirect(url_for('result'))
4178
42- @app .route ('/result' ,methods = ['GET' ,'POST' ])
79+ @app .route ('/result' , methods = ['GET' , 'POST' ])
4380def result ():
44- answer_file = loadAnswer ()
45- answer_key = paillier .PaillierPublicKey (n = int (answer_file ['pubkey' ]['n' ]))
46- answer = paillier .EncryptedNumber (answer_key , int (answer_file ['values' ][0 ]), int (answer_file ['values' ][1 ]))
81+ """
82+ Process the encrypted data to decrypt and display the predicted salary.
83+
84+ Steps:
85+ - Load encrypted answer from answer.json.
86+ - Decrypt the result using customer's private key.
87+ - Round the final result to two decimal places.
88+ - Render the result.html template with the final result.
89+
90+ Returns:
91+ Rendered result.html template with the final predicted salary.
92+ """
93+ # Load the encrypted answer data
94+ answer_file = loadAnswer ()
95+ # Initialize the public key from the answer file
96+ answer_key = paillier .PaillierPublicKey (n = int (answer_file ['pubkey' ]['n' ]))
97+ # Unpack the ciphertext and exponent
98+ ciphertext_str , exponent = answer_file ['values' ] # Changed from answer_file['values'][0]
99+ # Create an EncryptedNumber instance
100+ answer = paillier .EncryptedNumber (answer_key , int (ciphertext_str ), exponent )
101+ # Retrieve customer's keys
47102 pub_key , priv_key = getKeys ()
48- if (answer_key == pub_key ):
49- final_result = priv_key .decrypt (answer )
50-
103+ if answer_key .n == pub_key .n :
104+ # Decrypt the answer using the private key
105+ final_result = priv_key .decrypt (answer )
106+ # Round the result to two decimal places
107+ final_result = round (final_result , 2 ) # Rounded to 2 decimals
108+ else :
109+ final_result = "Keys do not match."
51110
52- return render_template ('result.html' ,final_result = final_result )
111+ # Render the result page with the final predicted salary
112+ return render_template ('result.html' , final_result = final_result )
53113
54-
55114if __name__ == "__main__" :
56- app .run (debug = True )
115+ # Run the Flask app with debug mode disabled for production
116+ app .run (debug = False )
0 commit comments