-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordManager.py
More file actions
200 lines (131 loc) · 5.21 KB
/
PasswordManager.py
File metadata and controls
200 lines (131 loc) · 5.21 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#Import
import os
import json
import tkinter as tk
from tkinter import messagebox
from cryptography.fernet import Fernet
import random
import string
#Key
# Function to generate a key and save it into a file
def generate_key():
key = Fernet.generate_key()
with open("key.key", "wb") as key_file:
key_file.write(key)
# Function to load the key from the current directory named `key.key`
def load_key():
return open("key.key", "rb").read()
# Generate and load key
if not os.path.exists("key.key"):
generate_key()
key = load_key()
cipher_suite = Fernet(key)
#Encryption and decryption
# Function to encrypt a password
def encrypt_password(password):
encrypted_password = cipher_suite.encrypt(password.encode())
return encrypted_password
# Function to decrypt a password
def decrypt_password(encrypted_password):
decrypted_password = cipher_suite.decrypt(encrypted_password).decode()
return decrypted_password
#Password storage
# Function to add a password
def add_password(service, password):
encrypted_password = encrypt_password(password)
with open("passwords.json", "r") as file:
passwords = json.load(file)
passwords[service] = encrypted_password.decode()
with open("passwords.json", "w") as file:
json.dump(passwords, file)
# Function to get a password
def get_password(service):
with open("passwords.json", "r") as file:
passwords = json.load(file)
encrypted_password = passwords.get(service)
if encrypted_password:
return decrypt_password(encrypted_password.encode())
else:
return None
#Generate
# Initialize the passwords file
if not os.path.exists("passwords.json"):
with open("passwords.json", "w") as file:
json.dump({}, file)
# Function to generate a random password
def generate_random_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
# ui tk
# Create the main application window
class PasswordManagerApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("Password Manager")
self.geometry("400x350")
# Show password variable
self.show_password = tk.BooleanVar()
# Service Label and Entry
self.service_label = tk.Label(self, text="Service:")
self.service_label.pack(pady=5)
self.service_entry = tk.Entry(self)
self.service_entry.pack(pady=5)
# Password Label and Entry
self.password_label = tk.Label(self, text="Password:")
self.password_label.pack(pady=5)
self.password_entry = tk.Entry(self, show="*")
self.password_entry.pack(pady=5)
# Show Password Button
self.show_password_button = tk.Checkbutton(self, text="Show Password", variable=self.show_password, command=self.toggle_password)
self.show_password_button.pack(pady=5)
# Add Password Button
self.add_button = tk.Button(self, text="Add Password", command=self.add_password)
self.add_button.pack(pady=5)
# Generate Password Button
self.generate_button = tk.Button(self, text="Generate Password", command=self.generate_password)
self.generate_button.pack(pady=5)
# Get Password Button
self.get_button = tk.Button(self, text="Get Password", command=self.get_password)
self.get_button.pack(pady=5)
def toggle_password(self):
if self.show_password.get():
self.password_entry.config(show="")
else:
self.password_entry.config(show="*")
def add_password(self):
service = self.service_entry.get()
password = self.password_entry.get()
if service and password:
add_password(service, password)
messagebox.showinfo("Success", "Password added successfully!")
self.service_entry.delete(0, tk.END)
self.password_entry.delete(0, tk.END)
else:
messagebox.showwarning("Input Error", "Please enter both service and password.")
def generate_password(self):
service = self.service_entry.get()
if service:
password = generate_random_password()
self.password_entry.delete(0, tk.END)
self.password_entry.insert(0, password)
add_password(service, password)
messagebox.showinfo("Generated Password", f"Generated password for {service}: {password}")
else:
messagebox.showwarning("Input Error", "Please enter the service name.")
def get_password(self):
service = self.service_entry.get()
if service:
password = get_password(service)
if password:
self.password_entry.delete(0, tk.END)
self.password_entry.insert(0, password)
messagebox.showinfo("Password Retrieved", f"The password for {service} is {password}")
else:
messagebox.showwarning("Not Found", "Service not found.")
else:
messagebox.showwarning("Input Error", "Please enter the service name.")
# Run the application
if __name__ == "__main__":
app = PasswordManagerApp()
app.mainloop()