🔐 Project Overview
In this project, you will create a secure Password Manager that :

✔ Saves usernames & passwords
✔ Encrypts all data
✔ Stores everything in a JSON file
✔ Allows you to add and retrieve credentials
✔ Uses a master password
✔ Real-world usefulness (similar to LastPass / 1Password basics)
You will learn:
- Encryption using Fernet (cryptography module)
- JSON handling
- File operations
- Secure input (getpass)
- Functions, loops, dictionaries
⚙️ 1. Setup Requirements
Before running the project, install cryptography:
pip install cryptography
Create these two files:
1️⃣ password_manager.py
2️⃣ vault.json (empty JSON file)
🧾 2. Full Code: password_manager.py
import json
import os
from cryptography.fernet import Fernet
from getpass import getpass
# ----------- KEY MANAGEMENT -----------
def load_key():
if os.path.exists("key.key"):
with open("key.key", "rb") as key_file:
return key_file.read()
else:
key = Fernet.generate_key()
with open("key.key", "wb") as key_file:
key_file.write(key)
return key
key = load_key()
fernet = Fernet(key)
# ----------- LOAD & SAVE ENCRYPTED DATA -----------
def load_data():
if os.path.exists("vault.json"):
with open("vault.json", "rb") as file:
encrypted = file.read()
if encrypted:
try:
decrypted = fernet.decrypt(encrypted)
return json.loads(decrypted)
except:
return {}
return {}
return {}
def save_data(data):
encrypted = fernet.encrypt(json.dumps(data).encode())
with open("vault.json", "wb") as file:
file.write(encrypted)
# ----------- PASSWORD MANAGER FUNCTIONS -----------
def add_password(vault):
website = input("Website: ").strip()
username = input("Username: ").strip()
password = getpass("Password (hidden): ")
vault[website] = {"username": username, "password": password}
save_data(vault)
print("✔ Password saved successfully!\n")
def view_password(vault):
website = input("Enter website name: ").strip()
if website in vault:
print("\n🔐 Stored Credentials:")
print(f"Website: {website}")
print(f"Username: {vault[website]['username']}")
print(f"Password: {vault[website]['password']}\n")
else:
print("❌ No credentials found for this website.\n")
def list_all(vault):
if not vault:
print("No passwords stored.\n")
return
print("\n📜 Saved Websites:")
for site in vault.keys():
print(f"- {site}")
print()
# ----------- MASTER PASSWORD SYSTEM -----------
MASTER_PASSWORD = "admin123" # <-- You can change this
def check_master():
attempts = 3
while attempts > 0:
pwd = getpass("Enter Master Password: ")
if pwd == MASTER_PASSWORD:
print("✔ Access granted!\n")
return True
attempts -= 1
print("❌ Wrong password. Attempts left:", attempts)
print("🚫 Access denied.")
exit()
# ----------- MAIN MENU -----------
def main():
check_master()
vault = load_data()
while True:
print("🔐 Password Manager")
print("1. Add Password")
print("2. View Password")
print("3. List All Websites")
print("4. Exit")
choice = input("👉 Choose (1-4): ")
if choice == "1":
add_password(vault)
elif choice == "2":
view_password(vault)
elif choice == "3":
list_all(vault)
elif choice == "4":
print("Goodbye!")
break
else:
print("❌ Invalid choice.\n")
if __name__ == "__main__":
main()
🖥 3. How to Run
- Save the code in:
password_manager.py
- Create an empty file:
vault.json
- Open CMD inside the folder and run:
python password_manager.py
📌 4. Example Usage
🔑 Master Password
Enter Master Password:
✔ Access granted!
➕ Add a password
Website: github.com
Username: john
Password: ****
✔ Password saved successfully!
🔎 View a password
Enter website name: github.com
Website: github.com
Username: john
Password: mySecret123
📜 List all saved websites
- github.com
- gmail.com
🧠 5. Summary Table
| Feature | Description |
|---|---|
| Encryption | All data encrypted using Fernet |
| Master Password | Protects access |
| Add Password | Stores website, username, password |
| View Password | Decrypts and displays data |
| JSON Storage | Persistent encrypted storage |
| Hidden Input | Secure password entry |
🎉 Project Completed!
This Password Manager is a strong foundation for future upgrades.
⬇️ Download FiveG_Pro تحميل Server