-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex_Encryption.py
More file actions
47 lines (26 loc) · 868 Bytes
/
Copy pathIndex_Encryption.py
File metadata and controls
47 lines (26 loc) · 868 Bytes
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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
get_ipython().system('pip install faiss-cpu cryptography')
# In[ ]:
import faiss
from cryptography.fernet import Fernet
# In[ ]:
# Load existing FAISS index
index_path = "faiss.index" # change if needed
index = faiss.read_index(index_path)
print("Loaded FAISS index")
print("Total vectors:", index.ntotal)
index_bytes = faiss.serialize_index(index)
print("Index serialized (bytes length):", len(index_bytes))
# Generate encryption key
key = Fernet.generate_key()
with open("secret.key", "wb") as f:
f.write(key)
print("Encryption key saved: secret.key")
# Encrypt serialized index and save the same
cipher = Fernet(key)
encrypted_data = cipher.encrypt(index_bytes)
with open("faiss_encrypted.index", "wb") as f:
f.write(encrypted_data)
print("Encrypted index saved: faiss_encrypted.index")