← Back to list

Building a Secure Password Manager with Python and Encryption.

While Recently Learning a Python development course on Udemy, I stumbled across a fascinating project idea: creating a password manager…

Ike-Obasi Dilichi Micheal · 2024-10-27 10:43 · 12 claps · 3.5 min read
#encryption #breaching #password-management #brute-force-attack #python
Open on Medium ↗
Wiki topics: BIZ · Business Strategy EDU · Education & Learning 🔒 · Cybersecurity

Building a Secure Password Manager with Python and Encryption.

While Recently Learning a Python development course on Udemy, I stumbled across a fascinating project idea: creating a password manager. With data security becoming more essential than ever, a password manager seemed like a valuable project, one that would combine both practical coding skills and a deeper understanding of data protection. In this article, I’ll take you through the steps to build a basic password manager in Python, where we’ll use encryption to keep stored passwords safe.

Introduction

Today, a secure password manager is almost a necessity. With so many accounts and passwords to remember, managing them securely without having to rely on external tools feels both satisfying and educational. In this project, we’ll create a password manager in Python that stores passwords in an encrypted format. Using SQLite, we’ll handle local storage, and with the cryptography library, we'll add encryption and decryption capabilities.

Project Setup

Here are the main tools and libraries we’ll use:

  • SQLite for database storage.
  • cryptography library for encryption.

To get started, install cryptography by running:

pip install cryptography

We’ll set up the following files to keep our project organized:

  • password_manager.py — main script.
  • **database.py** — handles database operations.
  • **encryption.py** — takes care of encryption and decryption functions.

Step 1: Setting Up the Database

For our password manager, we need to store usernames and passwords securely. SQLite is a great option because it’s lightweight and easy to set up.

  1. Database Structure: The main table will be called passwords, with three fields:
  • service_name: the service (e.g., Gmail, Facebook).
  • username: the username used for the account.
  • password: the encrypted password.

2. Creating the Database Here’s a function to set up our SQLite database:

import sqlite3
def create_database():
    conn = sqlite3.connect('passwords.db')
    cursor = conn.cursor()
    cursor.execute('''CREATE TABLE IF NOT EXISTS passwords (
                         id INTEGER PRIMARY KEY,
                         service_name TEXT NOT NULL,
                         username TEXT NOT NULL,
                         password TEXT NOT NULL)''')
    conn.commit()
    conn.close()

With this, we’ve set up a table to hold the encrypted passwords securely.

Step 2: Encryption and Decryption with Cryptography

Now we’ll set up encryption to keep our passwords safe. Using Python’s cryptography library, we'll work with Fernet encryption, which is ideal for securing data locally.

  1. Setting Up the Encryption Key Fernet uses a symmetric key for encryption and decryption. Let’s generate a key:
from cryptography.fernet import Fernet
def generate_key():
    key = Fernet.generate_key()
    with open("key.key", "wb") as key_file:
        key_file.write(key)

To load this key:

def load_key():
 return open(“key.key”, “rb”).read()
  1. Encrypting and Decrypting Passwords, Now let’s write functions to encrypt and decrypt passwords:
def encrypt_password(password, key):
    f = Fernet(key)
    return f.encrypt(password.encode())

def decrypt_password(encrypted_password, key):
    f = Fernet(key)
    return f.decrypt(encrypted_password).decode()

With these in place, we can store passwords in the database in encrypted form and retrieve them securely.

Step 3: Core Functions of the Password Manager

The main functions of our password manager are adding, retrieving, updating, and deleting passwords.

  1. Adding a Password
def add_password(service, username, password):
    conn = sqlite3.connect('passwords.db')
    cursor = conn.cursor()
    key = load_key()
    encrypted_password = encrypt_password(password, key)
    cursor.execute('INSERT INTO passwords (service_name, username, password) VALUES (?, ?, ?)', (service, username, encrypted_password))
    conn.commit()
    conn.close()

2. Retrieving a Password

def retrieve_password(service, username):
    conn = sqlite3.connect('passwords.db')
    cursor = conn.cursor()
    key = load_key()
    cursor.execute('SELECT password FROM passwords WHERE service_name = ? AND username = ?', (service, username))
    result = cursor.fetchone()
    conn.close()
    if result:
        return decrypt_password(result[0], key)
    return None

3. Updating a Password

def update_password(service, username, new_password):
    conn = sqlite3.connect('passwords.db')
    cursor = conn.cursor()
    key = load_key()
    encrypted_password = encrypt_password(new_password, key)
    cursor.execute('UPDATE passwords SET password = ? WHERE service_name = ? AND username = ?', (encrypted_password, service, username))
    conn.commit()
    conn.close()

4. Deleting a Password

def delete_password(service, username):
    conn = sqlite3.connect('passwords.db')
    cursor = conn.cursor()
    cursor.execute('DELETE FROM passwords WHERE service_name = ? AND username = ?', (service, username))
    conn.commit()
    conn.close()

These functions enable us to manage passwords securely.

Optional: Building a User Interface

To enhance usability, you could add a CLI or a simple graphical user interface (GUI) using tkinter.

def main():
    print("Welcome to the Python Password Manager!")
    print("Options: add, retrieve, update, delete")
    # Add input handling for user command

Security Tips

  1. Store the Encryption Key Securely: Since the encryption key is critical, secure it in a key vault or an environment variable if possible.

2. Add a Master Password (Optional): A master password can add an additional layer of security to your password manager.

3. Additional Security Suggestions

  • Use hashed, salted passwords if you add a master password.
  • Implement multi-factor authentication if you’re planning to expand the project further.

Testing and Debugging

Testing and debugging are essential to ensure everything works as expected.

  1. Test Each Function: Confirm that all core functions — adding, retrieving, updating, and deleting passwords — work correctly with encrypted data.

2. Common Issues

  • Check that the encryption key is loaded correctly.
  • Debugging database connection issues is crucial.

Conclusion

Creating a password manager is a hands-on way to improve both Python and security skills. By following this guide, you’ll have built a fully functional, secure tool for managing your passwords. There’s plenty of room for expansion, too: consider adding features like cloud storage, a browser extension, or even multi-factor authentication to make your password manager more robust.

By building this password manager, you’re not only protecting your data but also gaining valuable experience in Python programming, database management, and data encryption. Enjoy the process, and keep experimenting!


메타데이터
post_id
2cb460e2a5aa
slug
building-a-secure-password-manager-with-python-and-encryption-2cb460e2a5aa
url
https://medium.com/@dilichi20044/building-a-secure-password-manager-with-python-and-encryption-2cb460e2a5aa
canonical_url
https://medium.com/@dilichi20044/building-a-secure-password-manager-with-python-and-encryption-2cb460e2a5aa
author_url
https://medium.com/@dilichi20044
status
ok
fetched_at
2026-07-23 09:30:10