-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpassword_Manager.py
278 lines (208 loc) · 7.79 KB
/
password_Manager.py
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import base64
import os
import random
import string
from cryptography.fernet import Fernet, InvalidToken
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
# Sample Space
sampleSpace = string.ascii_letters + string.digits + string.punctuation
# Functions
# For getting the credentials
def getCredentials():
# Credentials Input
websiteName = input(
"Enter the website name whose details you want to save:")
username = input("Enter the username/email for the site:")
passChoice = input(
"If you want to use a strong generated password type 1 or If you want to use your own password type 2:"
)
if passChoice == '1':
passLen=16
passLenChoice=input("Default password length is 16, To use a longer/shorter password type 'n' else press any other key:")
if passLenChoice.lower()=="n":
passLen = int(
input(
"Enter the length of the password that you want to use(e,g:8/10/69) [MAX:128]:"
))
if(passLen > 128):
passLen=16
print("Password length too big , reverting back to default size of 16.")
password=""
while passLen!=0:
password+=random.choice(sampleSpace)
passLen-=1
elif passChoice == '2':
password = input("Enter the password that you want to use:")
return [websiteName, username, password]
# For getting the master password
def getMasterPassword(case):
if case==1:
masterPassword = input(
"Enter a master password to store all your credentials(make sure you remember it):"
).encode()
if case==2:
masterPassword = input(
"Enter your master password to continue:").encode()
return masterPassword
# For deriving the key
def keyDeriving(masterPassword, salt=None):
# Making a salt file
if salt != None:
with open("salt.txt", "wb") as slt:
slt.write(salt)
#When the salt file is already present
elif salt == None:
try:
with open("salt.txt","rb") as slt:
salt = slt.read()
# If salt file is not found then it has not been created or is removed.
except FileNotFoundError:
print()
print(
"Error! No entries found! They have been either deleted or not created at the first place."
)
quit()
# One time process of deriving key from master password and salt.
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend())
new_key = base64.urlsafe_b64encode(kdf.derive(masterPassword))
return new_key
# For writing the data
def writeData(websiteName, username, password, mode):
s1 = '\n' + 'Site:' + websiteName + '\n'
s2 = 'User id:' + username + '\n'
s3 = 'Password:' + password + '\n'
# Writing the credentials to a text file.
with open("credentials.txt", mode) as file:
file.write(s1 + s2 + s3)
# For encryting the data
def encryptData(key, case):
f = Fernet(key)
# Encryption process
with open("credentials.txt") as file:
data = file.read()
encryptedData = f.encrypt(bytes(data, encoding='utf8'))
with open("credentials.txt", "w") as file:
file.write(encryptedData.decode())
if case == 1:
print("Your credentials have been safely stored and are encrypted.")
return
if case == 2:
quit()
if case == 3:
print("Encrypted")
# For decrypting the data
def decryptData(new_key):
f = Fernet(new_key)
with open("credentials.txt") as file:
encryptedData = file.read()
try:
decryptedData = f.decrypt(bytes(encryptedData, encoding='utf8'))
with open("credentials.txt", "w") as file:
file.write(decryptedData.decode())
return
except InvalidToken:
print()
print("Wrong password, please try again!")
quit()
# Help section
def helpSection():
print()
print(
"Right now,you are viewing the help section of PassBot(A simple yet quite effective password manager)"
)
print("If you are using this for the 1st time then type 'new' \n")
print(
"If you have already used this to save some passwords and want to view them ,then type 'old' and choose option 2"
)
print(
"If you have already used this and want to save another password,then type 'old' and choose 1"
)
print("You will now go back to the menu.")
print()
return
# Main program starts from here.
# Greetings!
print(
"Hello, welcome to PassBot. This is a simple,easy to use password manager,to store all your important credentials."
)
while True:
print("To know more, type 'help'")
print(
"If you are ready to use and this is your first time,Type 'New'\nIf already used before type 'Old'"
)
userChoice = input("Enter your choice:").lower()
if userChoice == 'new':
while True:
# prompt for ready
readyOrNot = input(
"Now we shall ask you for your credentials.When ready type 'ready' else type 'quit':"
)
# if ready
if readyOrNot.lower() == "ready":
# input of credentials
websiteName, username, password = [
str(x) for x in getCredentials()
]
# Input for master password
masterPassword = getMasterPassword(1)
# One time process
salt = os.urandom(16)
key = keyDeriving(masterPassword, salt)
# writing the data
writeData(websiteName, username, password, 'w')
# Encryption process
encryptData(key, 1)
break
elif readyOrNot.lower() == 'quit':
quit()
else:
print("Wrong Choice")
break
if userChoice == 'old':
print(
"To enter new credentials type 1\nTo view saved passwords type 2:")
manageOrStore = input("Enter your choice:")
# If user wants to enter new data
if manageOrStore == '1':
masterPassword=getMasterPassword(2)
new_key = keyDeriving(masterPassword)
decryptData(new_key)
while True:
readyOrNot = input(
"Now we shall ask you for your credentials.When ready type 'ready' else type 'quit': "
)
if readyOrNot.lower() == "ready":
websiteName, username, password = [
str(x) for x in getCredentials()
]
writeData(websiteName, username, password, 'a')
encryptData(new_key, 1)
break
# If user wants to quit
elif readyOrNot.lower() == 'quit':
encryptData(new_key, 2)
# If user wants to view stored data
if manageOrStore == '2':
masterPassword=getMasterPassword(2)
new_key = keyDeriving(masterPassword)
decryptData(new_key)
print(
"The file is now decrypted and you can go to it to see your credentials."
)
while True:
inp = input("When done type 'encrypt': ")
if inp.lower() == 'encrypt':
encryptData(new_key, 3)
break
break
if userChoice == 'help':
helpSection()
else:
print("Wrong Choice, you will be sent to the help section now")
print()