-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpassword_manager.py
More file actions
80 lines (64 loc) · 1.95 KB
/
password_manager.py
File metadata and controls
80 lines (64 loc) · 1.95 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
import os
from cryptography.fernet import Fernet
# Specify the file name you want to check
key_file_name = "key.key"
ouput_file_name = "passwords.txt"
# Get the current directory path
current_directory = os.getcwd()
# Empty key
fer = None
# Creating the key file to encyption
def write_key():
key = Fernet.generate_key()
with open(key_file_name, "wb") as key_file:
key_file.write(key)
print("New key file created")
# Loading the key file to decyption
def load_key():
with open(key_file_name, "rb") as key_file:
loaded_key = key_file.read()
print("configuring the key")
return loaded_key
# To view the records
def view():
output_file_path = os.path.join(current_directory, ouput_file_name)
if os.path.isfile(output_file_path):
with open(ouput_file_name, 'r') as f:
lines_in_file = f.readlines()
# Check if there is at least one non-empty line
if len(lines_in_file) > 0:
for line in lines_in_file:
data = line.rstrip()
user, passw = data.split("|")
print("User:", user, "| Password:", fer.decrypt(passw.encode()).decode())
else:
print('No entries')
else:
print("Add atleast one entry")
# To add atleast one entry
def add():
name = input('Account Name: ')
pwd = input("Password: ")
with open(ouput_file_name, 'a') as f:
f.write(name + "|" + fer.encrypt(pwd.encode()).decode() + "\n")
print("Details added")
# Combine the current directory path with the file name
file_path = os.path.join(current_directory, key_file_name)
# Check if the file exists
if os.path.isfile(file_path):
key = load_key()
fer = Fernet(key)
else:
write_key()
while True:
if fer is not None:
mode = input(
"Would you like to add a new password or view existing ones (view, add), press q to quit? ").lower()
if mode == 'q':
break
if mode in ["view", "v"]:
view()
elif mode in ["add", "a"]:
add()
else:
continue