-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
239 lines (190 loc) · 6.81 KB
/
project.py
File metadata and controls
239 lines (190 loc) · 6.81 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from cryptography.fernet import Fernet
from time import sleep
import csv
import getpass
import os
import random
import string
class Encryptor():
def create_key(self):
key = Fernet.generate_key()
return key
def write_key(self, key, key_name):
with open(key_name, 'wb') as mykey:
mykey.write(key)
def load_key(self, key_name):
with open(key_name, 'rb') as mykey:
key = mykey.read()
return key
def file_encryption(self, key, original_file, encrypted_file):
fernet = Fernet(key)
with open(original_file, 'rb') as file:
original_file = file.read()
encrypted = fernet.encrypt(original_file)
with open(encrypted_file, 'wb') as file:
file.write(encrypted)
def file_decrypt(self, key, encrypted_file, decrypted_file):
f = Fernet(key)
with open(encrypted_file, 'rb') as file:
encrypted = file.read()
decrypted = f.decrypt(encrypted)
with open(decrypted_file, 'wb') as file:
file.write(decrypted)
print("Welcome to Password Manager")
def main():
print("""
Choose an option(1 or 2):
(1) Sign In
(2) Sign Up
(q) Quit
""")
try:
users_input = input(">>> ").lower()
if users_input == "1":
profile()
elif users_input == "2":
create_profile()
elif users_input == "":
print("No valid input")
sleep(0.5)
main()
elif users_input == "q" or "quit":
print("Goodbye!")
quit()
except KeyboardInterrupt:
print("\nProgram terminated!")
quit()
def menu():
sleep(1)
print("""
Please choose from the following option:
(1) Add a passwords
(2) View saved passwords
(3) Generate a random password
(q) Sign Out
""")
try:
users_input = input(">>> ").lower()
if users_input == "1":
add_password()
elif users_input == "2":
print(view_password())
elif users_input == "3":
generate_password()
elif users_input == "":
print("No valid input")
sleep(0.5)
menu()
elif users_input == "q" or "quit":
print("Signed Out!")
sleep(0.5)
main()
except KeyboardInterrupt:
print("\nProgram terminated!")
quit()
def create_profile():
global user
user = input("Username: ")
generate_key(user)
password = getpass.getpass("Password: ")
with open(f"{user}_creds.csv", "w") as file:
write_file = csv.writer(file, delimiter=",")
write_file.writerow([user, password])
encrypt = Encryptor()
key = encrypt.load_key(f"{user}.key")
encrypt.file_encryption(key, f"{user}_creds.csv", f"{user}_creds.csv")
print("Profile created!")
menu()
def profile():
global user
user = input("Username: ")
decrypt = Encryptor()
if not os.path.exists(f"{user}.key"):
print("User does not exist!")
sleep(0.5)
main()
else:
key = decrypt.load_key(f"{user}.key")
decrypt.file_decrypt(key, f"{user}_creds.csv", f"{user}_creds.csv")
password = getpass.getpass("Password: ")
file = open(f"{user}_creds.csv", 'r')
for i in file:
name, pwd = i.split(',')
pwd = pwd.strip()
if name == user and pwd == password:
print("Login Successful")
decrypt.file_encryption(key, f"{user}_creds.csv", f"{user}_creds.csv")
menu()
else:
print("Login failed!")
decrypt.file_encryption(key, f"{user}_creds.csv", f"{user}_creds.csv")
main()
def generate_key(username):
key_generation = Encryptor()
if os.path.exists(f'{username}.key'):
print("User already exists!")
sleep(0.5)
else:
user_key = key_generation.create_key()
key_generation.write_key(user_key, f'{username}.key')
pass_file = open(f"{username}_passwords.csv", "w")
pass_file.close()
encryptor = Encryptor()
key_file = encryptor.load_key(f'{username}.key')
encryptor.file_encryption(key_file, f'{username}_passwords.csv', f'{username}_passwords.csv')
def add_password():
try:
encryptor = Encryptor()
load_key = encryptor.load_key(f'{user}.key')
encryptor.file_decrypt(load_key, f'{user}_passwords.csv', f'{user}_passwords.csv')
website = input("Enter site: ").capitalize()
user_name = input("Enter Username/Email: ")
password = input("Enter password: ")
with open(f"{user}_passwords.csv", "a") as passfile:
write_pass = csv.writer(passfile, delimiter=',')
write_pass.writerow([website, user_name, password])
print("Information saved!")
encryptor.file_encryption(load_key, f'{user}_passwords.csv', f'{user}_passwords.csv')
menu()
except FileNotFoundError:
print("File not found, please select the first option to create the necessary files to continue!")
menu()
def view_password():
try:
decryptor = Encryptor()
load_key = decryptor.load_key(f'{user}.key')
decryptor.file_decrypt(load_key, f'{user}_passwords.csv', f'{user}_passwords.csv')
with open(f'{user}_passwords.csv', 'r') as file:
print(file.read())
print("Hit Enter to return to the menu")
go_back = input(">>> ")
while True:
if go_back == "":
decryptor.file_encryption(load_key, f'{user}_passwords.csv', f'{user}_passwords.csv')
menu()
else:
view_password()
except FileNotFoundError:
print("File not found, please select the first option to create the necessary files to continue!")
menu()
def generate_password():
try:
length = int(input("How long would you like your password? "))
if length <= 7:
print("Too short for a password!")
generate_password()
else:
random_password = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(length))
print(random_password)
except ValueError:
print("Invalid Input!")
generate_password()
print("Hit Enter to return to the menu")
menu_return = input(">>> ")
while True:
if menu_return == "":
menu()
else:
generate_password()
if __name__ == "__main__":
main()