-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure-note.py
More file actions
181 lines (154 loc) · 6.4 KB
/
secure-note.py
File metadata and controls
181 lines (154 loc) · 6.4 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
import cryptography
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
import shutil
class SecureNote:
def __init__(self):
self.private_key = None
self.public_key = None
self.notebook = Notebook()
self.storage_directory = "secure_notes"
os.makedirs(self.storage_directory, exist_ok=True)
def generate_keys(self):
self.private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
self.public_key = self.private_key.public_key()
def save_keys(self, private_key_path, public_key_path):
private_pem = self.private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
with open(private_key_path, "wb") as private_file:
private_file.write(private_pem)
public_pem = self.public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
with open(public_key_path, "wb") as public_file:
public_file.write(public_pem)
def load_keys(self, private_key_path, public_key_path):
with open(private_key_path, "rb") as private_file:
private_pem = private_file.read()
self.private_key = serialization.load_pem_private_key(private_pem, password=None, backend=default_backend())
with open(public_key_path, "rb") as public_file:
public_pem = public_file.read()
self.public_key = serialization.load_pem_public_key(public_pem, backend=default_backend())
def encrypt(self, plaintext):
ciphertext = self.public_key.encrypt(
plaintext.encode(),
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return ciphertext
def decrypt(self, ciphertext):
plaintext = self.private_key.decrypt(
ciphertext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return plaintext.decode()
def add_note(self, message):
ciphertext = self.encrypt(message)
self.notebook.add_note(ciphertext)
def delete_note(self, index):
self.notebook.delete_note(index)
def get_notes(self):
notes = self.notebook.get_notes()
decrypted_notes = [self.decrypt(note) for note in notes]
return decrypted_notes
def save_to_file(self, filename, content):
file_path = os.path.join(self.storage_directory, filename)
with open(file_path, "wb") as file:
file.write(content)
def load_from_file(self, filename):
file_path = os.path.join(self.storage_directory, filename)
with open(file_path, "rb") as file:
content = file.read()
return content
def list_files(self):
files = os.listdir(self.storage_directory)
return files
def export_file(self, filename, destination):
file_path = os.path.join(self.storage_directory, filename)
shutil.copy(file_path, destination)
def delete_file(self, filename):
file_path = os.path.join(self.storage_directory, filename)
os.remove(file_path)
def export_notes(self, with_password=False, password=None):
notes = self.get_notes()
if with_password:
decrypted_notes = []
for note in notes:
try:
decrypted_note = self.decrypt(note)
decrypted_notes.append(decrypted_note)
except cryptography.exceptions.InvalidKey:
print("پسورد نادرست. رمزگشایی ناموفق.")
return None
return decrypted_notes
else:
return notes
class Notebook:
def __init__(self):
self.notes = []
def add_note(self, note):
self.notes.append(note)
def delete_note(self, index):
if index >= 0 and index < len(self.notes):
del self.notes[index]
def get_notes(self):
return self.notes
note_app = SecureNote()
note_app.generate_keys()
note_app.save_keys("private_key.pem", "public_key.pem")
note_app.load_keys("private_key.pem", "public_key.pem")
while True:
print("1. اضافه کردن یادداشت")
print("2. حذف یادداشت")
print("3. نمایش یادداشتها")
print("4. گرفتن یادداشتها (با پسورد)")
print("5. گرفتن یادداشتها (بدون پسورد)")
print("6. خروج")
choice = input("لطفاً شماره گزینه را وارد کنید: ")
if choice == "1":
message = input("لطفا متن یادداشت خود را وارد کنید: ")
note_app.add_note(message)
print("یادداشت اضافه شد.")
elif choice == "2":
index = int(input("لطفاً شماره یادداشت مورد نظر برای حذف را وارد کنید: "))
note_app.delete_note(index - 1)
print("یادداشت حذف شد.")
elif choice == "3":
notes = note_app.get_notes()
print("لیست یادداشتها:")
for i, message in enumerate(notes):
print(f"{i + 1}. {message}")
elif choice == "4":
password = input("لطفاً پسورد را وارد کنید: ")
notes = note_app.export_notes(with_password=True, password=password)
if notes is not None:
print("یادداشتها (رمزگشایی با پسورد):")
for i, message in enumerate(notes):
print(f"{i + 1}. {message}")
elif choice == "5":
notes = note_app.export_notes(with_password=False)
if notes is not None:
print("یادداشتها (بدون رمزگشایی):")
for i, message in enumerate(notes):
print(f"{i + 1}. {message}")
elif choice == "6":
break
else:
print("گزینه نامعتبر. لطفاً دوباره وارد کنید.")