-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure_notes.py
More file actions
142 lines (109 loc) · 3.65 KB
/
secure_notes.py
File metadata and controls
142 lines (109 loc) · 3.65 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
import os
import json
import base64
import getpass
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
DATA_FILE = "secure_notes_data.json"
# -------------------------
# Key Derivation
# -------------------------
def derive_key(password: str, salt: bytes) -> bytes:
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=200_000,
)
return kdf.derive(password.encode())
# -------------------------
# Encryption
# -------------------------
def encrypt(text: str, password: str):
salt = os.urandom(16)
key = derive_key(password, salt)
aesgcm = AESGCM(key)
nonce = os.urandom(12)
ciphertext = aesgcm.encrypt(nonce, text.encode(), None)
return {
"salt": base64.b64encode(salt).decode(),
"nonce": base64.b64encode(nonce).decode(),
"ciphertext": base64.b64encode(ciphertext).decode(),
}
def decrypt(enc_data: dict, password: str) -> str:
salt = base64.b64decode(enc_data["salt"])
nonce = base64.b64decode(enc_data["nonce"])
ciphertext = base64.b64decode(enc_data["ciphertext"])
key = derive_key(password, salt)
aesgcm = AESGCM(key)
decrypted = aesgcm.decrypt(nonce, ciphertext, None)
return decrypted.decode()
# -------------------------
# Storage
# -------------------------
def load_notes():
if not os.path.exists(DATA_FILE):
return []
with open(DATA_FILE, "r") as f:
return json.load(f)
def save_notes(notes):
with open(DATA_FILE, "w") as f:
json.dump(notes, f, indent=4)
# -------------------------
# CLI
# -------------------------
def main():
print("🔐 Secure Notes (Single File Version)")
password = getpass.getpass("Enter your encryption password: ")
notes = load_notes()
while True:
print("\n1) Create Note")
print("2) View Notes")
print("3) Delete Note")
print("4) Exit")
choice = input("Choose: ").strip()
if choice == "1":
title = input("Title: ")
print("Enter note content. End with single line: .end")
lines = []
while True:
line = input()
if line.strip() == ".end":
break
lines.append(line)
content = "\n".join(lines)
encrypted = encrypt(content, password)
notes.append({"title": title, "data": encrypted})
save_notes(notes)
print("✅ Note saved securely.")
elif choice == "2":
if not notes:
print("No notes found.")
continue
for i, note in enumerate(notes):
print(f"{i+1}) {note['title']}")
try:
index = int(input("Select note number: ")) - 1
decrypted = decrypt(notes[index]["data"], password)
print("\n----- NOTE -----")
print(decrypted)
print("----------------")
except Exception:
print("❌ Wrong password or invalid selection.")
elif choice == "3":
for i, note in enumerate(notes):
print(f"{i+1}) {note['title']}")
try:
index = int(input("Delete note number: ")) - 1
notes.pop(index)
save_notes(notes)
print("🗑 Note deleted.")
except:
print("Invalid selection.")
elif choice == "4":
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()