11 – Real-World Python Projects – Password Manager

๐ŸŽฏ Project Objective

To build a secure Password Manager in Python that can store, retrieve, and generate passwords safely.
Skills Demonstrated:

  • File handling and data storage
  • Encryption for sensitive information
  • GUI for user-friendly interaction (optional)
  • Random password generation

Project: Password Manager

Project Description

The Password Manager app allows users to:

  • Add new passwords with website, username, and password
  • Retrieve saved passwords
  • Generate strong passwords automatically
  • Securely store data in an encrypted format

Use Cases:

  • Personal password storage
  • Generate strong, unique passwords
  • Safely retrieve credentials when needed

Python Example Code โ€“ Basic Password Manager (Console Version)

import json
import random
import string
from cryptography.fernet import Fernet
import os

# Generate a key (do this once and save it)
key_file = "secret.key"
if not os.path.exists(key_file):
    key = Fernet.generate_key()
    with open(key_file, "wb") as f:
        f.write(key)
else:
    with open(key_file, "rb") as f:
        key = f.read()

cipher = Fernet(key)

# File to store passwords
password_file = "passwords.json"

# Load existing passwords
if os.path.exists(password_file):
    with open(password_file, "r") as f:
        passwords = json.load(f)
else:
    passwords = {}

# Function to generate random password
def generate_password(length=12):
    chars = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(chars) for _ in range(length))

# Add new password
def add_password(website, username, pwd=None):
    if not pwd:
        pwd = generate_password()
    encrypted_pwd = cipher.encrypt(pwd.encode()).decode()
    passwords[website] = {"username": username, "password": encrypted_pwd}
    with open(password_file, "w") as f:
        json.dump(passwords, f)
    print(f"Password saved for {website}: {pwd}")

# Retrieve password
def get_password(website):
    if website in passwords:
        data = passwords[website]
        decrypted_pwd = cipher.decrypt(data["password"].encode()).decode()
        print(f"Website: {website}\nUsername: {data['username']}\nPassword: {decrypted_pwd}")
    else:
        print("No password found for this website.")

# Example usage
add_password("gmail.com", "myemail@gmail.com")
get_password("gmail.com")

โœ… Output:

  • Stores encrypted passwords in passwords.json
  • Generates strong random passwords
  • Retrieves decrypted credentials when needed

โœ… Key Features

  • Generate strong passwords automatically
  • Encrypt and decrypt passwords securely
  • Store credentials in JSON file
  • Retrieve credentials with decryption
  • Extendable to GUI or web app

    Learning Points

    • File I/O and JSON handling
    • Encryption using cryptography
    • Random password generation
    • Basics of secure password storage
    • Building practical security-focused Python applications

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *