Skip to content

Commit 7224618

Browse files
authored
Merge pull request DhanushNehru#139 from nem5345/master
Added Password Manager for Issue DhanushNehru#138 Thanks @nem5345
2 parents 8c516a3 + 31e01d5 commit 7224618

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

Password Manager/README.MD

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Password Manager
2+
3+
## Usage
4+
Run the pwd_mgmr.py file and you will be prompted for the rest of the required inputs
5+
6+
### Getting Started:
7+
1. Select Option (1) Create a new key that will be used to encrypt your password file.
8+
2. Select Option (3) Create a new password file that will be used to hold your encrypted passwords.
9+
3. Select Option (5) Add a new password to the password file. \n
10+
11+
### Retrieving or Adding Passords:
12+
1. Select Option (2) Load the existing key so it can be used to encrypt new passwords or retrieve passwords from the password file.
13+
2. Select Option (4) Load the existing password file so it can be used to add or retrieve passwords.
14+
3. Select Option (5) Add a new password to the password file.
15+
4. Select Option (6) Retrieve a password for a site.
16+
17+
## Example
18+
19+
What would you like to do?
20+
(1) Create a new key
21+
(2) Load an existing key
22+
(3) Create new password file
23+
(4) Load existing password file
24+
(5) Add a new password
25+
(6) Get a password for a site
26+
(7) Get the list of sites
27+
(m) Menu
28+
(h) Help
29+
(q) Quit
30+
Enter your choice: h
31+
Getting Started:
32+
1. Select Option (1) Create a new key that will be used to encrypt your password file.
33+
2. Select Option (3) Create a new password file that will be used to hold your encrypted passwords.
34+
3. Select Option (5) Add a new password to the password file.
35+
36+
Retrieving or Adding Passords:
37+
1. Select Option (2) Load the existing key so it can be used to encrypt new passwords or retrieve passwords from the password file.
38+
2. Select Option (4) Load the existing password file so it can be used to add or retrieve passwords.
39+
3a. Select Option (5) Add a new password to the password file.
40+
3b. Select Option (6) Retrieve a password for a site.
41+
Enter your choice: 1
42+
Invalid Choice!
43+
Enter your choice: 1
44+
Enter the path: C:\pwds\mykey.key
45+
Enter your choice: 3
46+
Enter the path: C:\pwds\pwds.pass
47+
Enter your choice: 5
48+
Enter the site: facebook
49+
Enter the password: password123
50+
Enter your choice: 6
51+
What site do you want: facebook
52+
password123
53+
Enter your choice: 7
54+
List of Sites:
55+
facebook
56+
Enter your choice: q
57+
Bye!

Password Manager/pwd_mgmr.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from cryptography.fernet import Fernet
2+
# Symmetric Key Encyrption Class
3+
4+
# Password Manager Class that will create & load passwords from an encrypted file
5+
class PasswordManager:
6+
def __init__(self):
7+
self.key = None
8+
self.pwd_file = None
9+
self.pwd_dict = {}
10+
11+
def create_key(self, path):
12+
self.key = Fernet.generate_key()
13+
with open(path, 'wb') as f:
14+
f.write(self.key)
15+
16+
def load_key(self, path):
17+
with open(path, 'rb') as f:
18+
self.key = f.read()
19+
20+
# init_values is a dictionary
21+
def create_pwd_file(self, path, init_values=None):
22+
self.pwd_file = path
23+
if init_values is not None:
24+
for key, values in init_values.items():
25+
self.add_password(self, key, values)
26+
27+
def load_pwd_file(self, path):
28+
self.pwd_file = path
29+
with open(path, 'r') as f:
30+
for line in f:
31+
site, encrypted = line.split(":")
32+
# Loads the site and the associated encrypted password. Password must be encoded before decyprtion and decoded before returning text
33+
self.pwd_dict[site] = Fernet(self.key).decrypt(encrypted.encode()).decode()
34+
35+
def add_password(self, site, password):
36+
self.pwd_dict[site] = password
37+
if self.pwd_file is not None:
38+
with open(self.pwd_file, 'a') as f:
39+
encrypted = Fernet(self.key).encrypt(password.encode())
40+
s = ":"
41+
written = site + s + encrypted.decode() + "\n"
42+
f.write(written)
43+
44+
def get_password(self, site):
45+
return self.pwd_dict[site]
46+
47+
def get_sites(self):
48+
print("List of Sites:")
49+
for a in self.pwd_dict.keys():
50+
print(a)
51+
52+
def main():
53+
pm = PasswordManager()
54+
print("""What would you like to do?
55+
(1) Create a new key
56+
(2) Load an existing key
57+
(3) Create new password file
58+
(4) Load existing password file
59+
(5) Add a new password
60+
(6) Get a password for a site
61+
(7) Get the list of sites
62+
(m) Menu
63+
(h) Help
64+
(q) Quit""")
65+
done = False
66+
67+
while not done:
68+
69+
choice = input("Enter your choice: ")
70+
choice = choice.lower()
71+
match choice:
72+
case "1":
73+
path = input("Enter the path: ")
74+
pm.create_key(path)
75+
case "2":
76+
path = input("Enter the path: ")
77+
pm.load_key(path)
78+
case "3":
79+
path = input("Enter the path: ")
80+
pm.create_pwd_file(path, init_values=None)
81+
case "4":
82+
path = input ("Enter the path: ")
83+
pm.load_pwd_file(path)
84+
case "5":
85+
site = input("Enter the site: ")
86+
password = input("Enter the password: ")
87+
pm.add_password(site, password)
88+
case "6":
89+
site = input("What site do you want: ")
90+
print(pm.get_password(site))
91+
case "7":
92+
pm.get_sites()
93+
case "m":
94+
print("""What would you like to do?
95+
(1) Create a new key
96+
(2) Load an existing key
97+
(3) Create new password file
98+
(4) Load existing password file
99+
(5) Add a new password
100+
(6) Get a password for a site
101+
(m) Menu
102+
(h) Help
103+
(q) Quit""")
104+
case "h":
105+
print("""Getting Started:
106+
1. Select Option (1) Create a new key that will be used to encrypt your password file.
107+
2. Select Option (3) Create a new password file that will be used to hold your encrypted passwords.
108+
3. Select Option (5) Add a new password to the password file. \n
109+
Retrieving or Adding Passords:
110+
1. Select Option (2) Load the existing key so it can be used to encrypt new passwords or retrieve passwords from the password file.
111+
2. Select Option (4) Load the existing password file so it can be used to add or retrieve passwords.
112+
3a. Select Option (5) Add a new password to the password file.
113+
3b. Select Option (6) Retrieve a password for a site.""")
114+
case "q":
115+
done = True
116+
print("Bye!")
117+
case _:
118+
print("Invalid Choice!")
119+
120+
if __name__ == '__main__':
121+
main()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Incase there is something that needs to be followed while executing the python s
4343
| No Screensaver | [No Screensaver](https://github.com/DhanushNehru/Python-Scripts/tree/master/Noscreensaver) | Prevents screensaver from turning on. |
4444
| OTP Verification | [OTP Verification](https://github.com/DhanushNehru/Python-Scripts/tree/master/OTP%20%20Verify) | An OTP Verification Checker OTPVerification.py |
4545
| Password Generator | [Password Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Password%20Generator) | Generates a random password |
46+
| Password Manager | [Password Manager](https://github.com/nem5345/Python-Scripts/tree/master/Password%20Manager) | Generate and interact with a password manager |
4647
| PDF to Audio | [PDF to Audio](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20Audio) | Converts PDF to audio. |
4748
| Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/planetSimulation) | A simulation of several planets rotating around the sun. |
4849
| Popup Window | [Popup Window](https://github.com/DhanushNehru/Python-Scripts/tree/master/Display%20Popup%20Window) | Displaying a popup window DisplayPopupWindow.py |

0 commit comments

Comments
 (0)