-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword_Manager.py
More file actions
97 lines (63 loc) · 2.87 KB
/
Password_Manager.py
File metadata and controls
97 lines (63 loc) · 2.87 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
from cryptography.fernet import Fernet # This is a module which allow you to encryt text
# Fernet is a tool that allows you to encrypt and decrypt data safely.
# Generate key once and save it to a file
def write_key():
key = Fernet.generate_key() # key is a secret code used to lock and unlock (encrypt/decrypt) your passwords.
with open("key.key", "wb") as key_file:
key_file.write(key)
# Load the key from file
def load_key():
return open("key.key", "rb").read()
# Uncomment the line below only once to create the key file
# write_key()
# Creating a Fernet object
key = load_key()
fer = Fernet(key)
# This connects your program to the secret key.
# Now fer can be used to:
# Encrypt: fer.encrypt()
# Decrypt: fer.decrypt()
#Adding (Encrypting) a password
def add_passwords(account, password):
with open("passwords_encrypted.txt", "a") as f:
encrypted = fer.encrypt(password.encode()).decode()
f.write(f"{account}:{encrypted}\n")
print("Password added successfully!")
'''
password.encode() → converts your password (a normal string) into bytes (since Fernet works with bytes).
fer.encrypt(...) → turns it into unreadable gibberish (encrypted form).
.decode() → converts those bytes back into a string so we can store them in a text file.'''
# Viewing (Decrypting) passwords
def view_passwords():
with open("passwords_encrypted.txt", "r") as f:
for line in f:
account, encrypted = line.strip().split(":")
decrypted = fer.decrypt(encrypted.encode()).decode()
print(f"{account}:{decrypted}")
#Converts encrypted text back into bytes.
# Decrypts it using the secret key.
# Converts it back into a readable password string.
# try:
# with open("passss.txt", "r") as f:
# for line in f:
# account, pwd= line.strip().split(":")
# print(f"Account : {account}, Password : {pwd}")
# except FileNotFoundError:
# print("No Passwords found, Add one first.")
print("-----WLECOME TO YOUR PASSWORD MANAGER-----")
while True:
print("1. Add new Password")
print("2. View Passwords")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == "1":
account = input("Enter account name: ")
password = input("Enter Password: ")
add_passwords(account,password)
elif choice == "2":
view_passwords()
elif choice == "3":
print("Exiting Password Manager...")
break
else:
print("Invalid choice Try again.")