-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword_Manager.py
More file actions
192 lines (147 loc) · 5.77 KB
/
Copy pathPassword_Manager.py
File metadata and controls
192 lines (147 loc) · 5.77 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
## Import Modules
import os
from cryptography.fernet import Fernet
##------------------------------------------------------------------------
# Key Generation Function
def generate_key():
try:
key = Fernet.generate_key()
with open(key_file,'wb') as file :
file.write(key)
except:
print("\nError in generating the Key file !!!\n")
exit()
##------------------------------------------------------------------------
# Key Load Function
def load_key():
try:
with open(key_file, 'rb') as file:
key = file.read()
return key
except:
print("\nError in Loading the Key !!!\n")
exit()
##------------------------------------------------------------------------
# Create Account Function
def create_acc():
# Username with no leading and trailing white spaces
user_name = input("\nUsername : ").rstrip().lstrip()
# Checking username characters length
if user_name == '' :
print('Username required !!!')
return
elif len(user_name) < 3 or len(user_name) > 15 :
print("Only 3 - 15 characters allowed in Username")
return
else:
# Padding with " ", if length < 15
while len(user_name) < 15:
user_name = user_name + " "
user_pwd = input("Password : ")
if user_pwd == '':
print('User Password required !!!')
elif user_pwd.count(" ") != 0:
print("\nPassword can't have space!!!")
elif len(user_pwd) < 4 or len(user_pwd) > 12:
print("\nPassword must have 4 - 12 characters !!!")
else:
try:
with open(passwords_file, 'a') as file:
# Encrypt and store password
encrypt_pwd = master_key.encrypt(user_pwd.encode()).decode()
file.write(f"\n {user_name} | {encrypt_pwd}" )
print("\nUsername with Password saved Successfully.")
except:
print("\nError in Writing to 'passwords.txt' file !!!\n")
exit()
##------------------------------------------------------------------------
# View Data Function
def view_data():
line_number = 1
try:
with open(passwords_file, 'r') as file:
lines = file.readlines()
# Checking data
if len(lines) < 3:
print("\nNo Data !!!")
return
# Structred Display
print("\n----------------------------------------------")
print("|S.No.| Username | Password |\n----------------------------------------------")
for line in lines:
# Ignoring first 2 lines of file
if line_number < 3:
line_number += 1
continue
data = line.rstrip()
user_name, user_pwd = data.split(" | ")
# Decrypt and read password
decrypt_pwd = master_key.decrypt(user_pwd.encode()).decode()
# Padding with " ", if length < 12
while len(decrypt_pwd) < 12:
decrypt_pwd = decrypt_pwd + " "
if line_number < 12:
print(f"| {line_number - 2}. | {user_name} | {decrypt_pwd} |")
else:
print(f"| {line_number - 2}. | {user_name} | {decrypt_pwd} |")
line_number += 1
print("----------------------------------------------")
except:
print("\nError in Reading from 'passwords.txt' file !!!\n")
exit()
##------------------------------------------------------------------------
# Access Control
master_pwd = input("\n----- PASSWORD MANAGER -----\n\nMaster Password : ")
if master_pwd != '2023':
print("\nACCESS DECLINED !!!\nIncorrect Master Password.\n")
exit()
print("\nACCESS GRANTED !!!\nWelcome\n")
##------------------------------------------------------------------------
## Setting up Password_Manager_Files directory
workingDirectory = os.getcwd()
manager_dir = 'Password_Manager_Files'
path = os.path.join(workingDirectory, manager_dir)
##------------------------------------------------------------------------
# Creating output directory
try:
if not os.path.exists(path):
os.makedirs(path)
except:
print(f"\nError in Creating directory\n")
exit()
##------------------------------------------------------------------------
# Files
passwords_file = 'Password_Manager_Files/passwords.txt'
key_file = 'Password_Manager_Files/key_file.key'
##------------------------------------------------------------------------
# Initializing password.txt file
if (not os.path.exists(passwords_file)) or os.path.getsize(passwords_file) == 0:
try:
with open(passwords_file, 'a') as file:
file.write(" Username | Password \n------------------------------------------------------------------------------------" )
except:
print("\nError in Creating passwords.txt file !!!\n")
exit()
##------------------------------------------------------------------------
# Generate Key_file
if not os.path.exists(key_file):
generate_key()
##------------------------------------------------------------------------
# Setting up the KEY
key = load_key() + master_pwd.encode()
master_key = Fernet(key)
##------------------------------------------------------------------------
# Menu Driven Display
while True:
mode = input("\n----- PASSWORD MANAGER -----\n\n1. CREATE Account\n2. VIEW Data\n3. Quit\n\n --> ")
if mode == '1':
create_acc()
input("\nPress Enter to Continue....")
elif mode == '2':
view_data()
input("\n\nPress Enter to continue....")
elif mode == '3':
print("\nThank You !!!\n")
break
else:
input("\nInvalid Input\nPress Enter to continue....")