-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomerEncrypts.py
79 lines (62 loc) · 2.58 KB
/
CustomerEncrypts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import phe as paillier
import json
from sklearn.compose import ColumnTransformer
import pickle
import pandas as pd
def storeKeys():
# Generate Paillier public-private key pair
public_key, private_key = paillier.generate_paillier_keypair()
# Store keys in a dictionary
keys = {
'public_key': {'n': public_key.n},
'private_key': {'p': private_key.p, 'q': private_key.q}
}
# Save keys to a JSON file
with open('PaillierKeys/custkeys.json', 'w') as file:
json.dump(keys, file)
def getKeys():
# Load keys from JSON file
with open('PaillierKeys/custkeys.json', 'r') as file:
keys = json.load(file)
# Reconstruct Paillier public-private key pair
pub_key = paillier.PaillierPublicKey(n=int(keys['public_key']['n']))
priv_key = paillier.PaillierPrivateKey(pub_key, keys['private_key']['p'], keys['private_key']['q'])
return pub_key, priv_key
def serializeData(public_key, data):
# Encrypt data using the provided public key
encrypted_data_list = [public_key.encrypt(x) for x in data]
# Prepare data for serialization
encrypted_data = {
'public_key': {'n': public_key.n},
'values': [(str(x.ciphertext()), x.exponent) for x in encrypted_data_list]
}
# Serialize encrypted data
serialized = json.dumps(encrypted_data)
return serialized
def preprocessData(data):
# Load the ColumnTransformer object from file
with open('col_tnf.pkl', 'rb') as file:
col_tnf = pickle.load(file)
# Convert the input data to a DataFrame
columns = ['Company', 'TypeName', 'Ram', 'Weight', 'os', 'Cpu brand', 'ProcessorSpeed', 'Gpu brand', 'GpuModel', 'HDD', 'SSD', 'Hybrid', 'Flash_Storage', 'TouchScreen', 'IPSPanel', 'PPI']
data_df = pd.DataFrame(data, columns=columns)
# Transform the data using the loaded ColumnTransformer
trans_data = col_tnf.transform(data_df)
# Flatten the transformed data
new_data = trans_data.flatten()
return new_data
def main():
# Generate and store Paillier keys
storeKeys()
# Load Paillier keys
pub_key, priv_key = getKeys()
# Sample data
data = [['Dell', 'Notebook', 8, 2.00, 'Windows', 'Intel Core i7', 2.8, 'Nvidia', 1050.0, 0, 256, 0, 0, 0, 0, 141.0211998]]
# Preprocess the data
new_data = preprocessData(data)
# Serialize and save the preprocessed data
datafile = serializeData(pub_key, new_data)
with open('Encrypted_Data/data.json', 'w') as file:
json.dump(datafile, file)
if __name__ == "__main__":
main()