GT1

Security Solutions

Build Your Own Password Manager

A step-by-step guide to creating a secure Python password manager

Start Learning

Why Build Your Own Password Manager?

Enhanced Security

You control the encryption and storage, eliminating third-party risks.

Customizable

Tailor features to your specific needs and workflow.

Learning Experience

Gain practical skills in Python, encryption, and security best practices.

Step-by-Step Tutorial

1

Install Python on Windows

Before we begin coding, you'll need Python installed on your Windows machine.

  1. Visit python.org/downloads
  2. Download the latest Python 3.x version
  3. Run the installer
  4. Check "Add Python to PATH"
  5. Click "Install Now"
  6. After installation, verify by opening Command Prompt and typing python --version
Python installation
2

Set Up Your Project

Create a new directory for your password manager project and set up the basic files.

# Create project directory
mkdir password-manager
cd password-manager

# Create main Python file
touch password_manager.py

# Install required libraries
pip install cryptography

We'll use the cryptography library for secure encryption.

Project setup
3

Basic Password Manager Code

Here's the complete code for a basic but functional password manager:

from cryptography.fernet import Fernet
import json
import os

class PasswordManager:
    def __init__(self):
        self.key = None
        self.password_file = None
        self.password_dict = {}

    def create_key(self, path):
        self.key = Fernet.generate_key()
        with open(path, 'wb') as f:
            f.write(self.key)

    def load_key(self, path):
        with open(path, 'rb') as f:
            self.key = f.read()

    def create_password_file(self, path, initial_values=None):
        self.password_file = path
        
        if initial_values is not None:
            for key, value in initial_values.items():
                self.add_password(key, value)

    def load_password_file(self, path):
        self.password_file = path
        
        with open(path, 'r') as f:
            for line in f:
                site, encrypted = line.strip().split(":")
                self.password_dict[site] = Fernet(self.key).decrypt(encrypted.encode()).decode()

    def add_password(self, site, password):
        self.password_dict[site] = password
        
        if self.password_file is not None:
            with open(self.password_file, 'a') as f:
                encrypted = Fernet(self.key).encrypt(password.encode())
                f.write(f"{site}:{encrypted.decode()}\n")

    def get_password(self, site):
        return self.password_dict[site]

def main():
    pm = PasswordManager()
    
    print("""GT1 Password Manager
1. Create new key
2. Load existing key
3. Create new password file
4. Load existing password file
5. Add new password
6. Get a password
q. Quit
""")
    
    while True:
        choice = input("Enter your choice: ")
        if choice == "1":
            path = input("Enter path to save key: ")
            pm.create_key(path)
        elif choice == "2":
            path = input("Enter path of key: ")
            pm.load_key(path)
        elif choice == "3":
            path = input("Enter path to save password file: ")
            pm.create_password_file(path)
        elif choice == "4":
            path = input("Enter path of password file: ")
            pm.load_password_file(path)
        elif choice == "5":
            site = input("Enter the site name: ")
            password = input("Enter the password: ")
            pm.add_password(site, password)
        elif choice == "6":
            site = input("Enter the site name: ")
            print(f"Password for {site}: {pm.get_password(site)}")
        elif choice == "q":
            break
        else:
            print("Invalid choice!")

if __name__ == "__main__":
    main()
4

Save and Run Your Program

Now that you've written the code, let's save and run it:

  1. Save the file as password_manager.py
  2. Open Command Prompt in your project directory
  3. Run the program with python password_manager.py
  4. Follow the on-screen menu to create your encryption key and password file
  5. Start adding your passwords!

Make sure to keep your encryption key file safe - without it, you won't be able to decrypt your passwords!

Running Python code
5

Using Your Password Manager

Here's how to use your new password manager:

  • First run: Create a new key and password file
  • Subsequent runs: Load your existing key and password file
  • Add passwords for different websites/services
  • Retrieve passwords when needed
  • Keep your key file and password file in a secure location

Security Tip: Never share your key file or store it with your password file. Consider keeping it on a separate USB drive.

Password security
6

Bonus: Save and Run from a USB Drive

For portability and security, you can run your password manager from a USB drive using a portable Python version:

  1. Insert your USB drive and note its drive letter (e.g., E:)
  2. Create a folder on the USB drive (e.g., E:\PasswordManager)
  3. Download Portable Python from winpython.github.io
  4. Extract the Portable Python archive to your USB drive (e.g., E:\WinPython)
  5. Copy your password_manager.py file to the E:\PasswordManager folder
  6. Open the Portable Python folder on the USB drive and run WinPython Command Prompt.exe
  7. In the command prompt, navigate to your project folder (e.g., cd \PasswordManager)
  8. Install the required cryptography library with pip install cryptography
  9. Run the program with python password_manager.py
  10. Store your key and password files on the USB drive, but keep backups elsewhere

Security Tip: Always eject the USB drive properly to prevent data corruption, and consider encrypting the USB drive itself for extra security.

USB drive setup

Enhance Your Password Manager

Add a GUI

Use Tkinter or PyQt to create a graphical interface for your password manager.

Cloud Backup

Add functionality to securely backup your passwords to cloud storage.

Password Generator

Implement a strong password generator with customizable options.