The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a password manager with Python

Creating a basic password manager in Python involves several key functionalities, such as storing passwords securely, encrypting the passwords, and allowing the user to retrieve them later. Below is an implementation of a simple password manager using the cryptography library for encryption and a file to store the data.

Step-by-Step Password Manager in Python

  1. Install Required Libraries:

    First, you’ll need to install the cryptography package, which is used for encryption. You can install it using pip:

    bash
    pip install cryptography
  2. Password Manager Code:

    Here’s the Python code for a basic password manager:

python
from cryptography.fernet import Fernet import os import json # Function to generate a key and save it def generate_key(): """Generate a key for encryption and save it to a file.""" key = Fernet.generate_key() with open("key.key", "wb") as key_file: key_file.write(key) # Function to load the encryption key def load_key(): """Load the previously generated key.""" return open("key.key", "rb").read() # Function to encrypt passwords def encrypt_password(password, key): """Encrypt the password using the key.""" f = Fernet(key) encrypted_password = f.encrypt(password.encode()) return encrypted_password # Function to decrypt passwords def decrypt_password(encrypted_password, key): """Decrypt the password using the key.""" f = Fernet(key) decrypted_password = f.decrypt(encrypted_password).decode() return decrypted_password # Function to store the password securely def store_password(account, password, key): """Store the password for a specific account.""" encrypted_password = encrypt_password(password, key) # If the file doesn't exist, create it if not os.path.exists("passwords.json"): with open("passwords.json", "w") as file: json.dump({}, file) # Load existing data with open("passwords.json", "r") as file: data = json.load(file) # Store the password encrypted data[account] = encrypted_password.decode() # Save the updated data with open("passwords.json", "w") as file: json.dump(data, file) # Function to retrieve a password def retrieve_password(account, key): """Retrieve the password for a specific account.""" # Load the data if not os.path.exists("passwords.json"): print("No passwords stored.") return None with open("passwords.json", "r") as file: data = json.load(file) # Check if account exists if account in data: encrypted_password = data[account].encode() return decrypt_password(encrypted_password, key) else: print("Account not found.") return None # Main function to run the password manager def main(): """Main function to interact with the password manager.""" if not os.path.exists("key.key"): print("No encryption key found. Generating a new one.") generate_key() key = load_key() while True: print("nPassword Manager") print("1. Store a new password") print("2. Retrieve a password") print("3. Exit") choice = input("Enter choice: ") if choice == "1": account = input("Enter the account name: ") password = input("Enter the password: ") store_password(account, password, key) print(f"Password for {account} stored securely.") elif choice == "2": account = input("Enter the account name: ") password = retrieve_password(account, key) if password: print(f"Password for {account}: {password}") elif choice == "3": print("Exiting Password Manager.") break else: print("Invalid choice. Please try again.") if __name__ == "__main__": main()

Explanation of Code:

  1. generate_key():

    • This function generates a new key for encryption using Fernet.generate_key(). The key is saved to a file (key.key) so it can be used later for encrypting and decrypting passwords.

  2. load_key():

    • This function loads the previously generated encryption key from the file (key.key).

  3. encrypt_password():

    • This function takes a plaintext password and encrypts it using the loaded encryption key.

  4. decrypt_password():

    • This function decrypts the encrypted password and returns it in its original form.

  5. store_password():

    • This function stores the encrypted password in a JSON file (passwords.json), with the account name as the key. If the file doesn’t exist, it will be created.

  6. retrieve_password():

    • This function retrieves an encrypted password for a given account from the passwords.json file and decrypts it.

  7. User Interface:

    • The main loop presents a menu to the user where they can choose to store a new password, retrieve an existing one, or exit the program.

Running the Password Manager:

  • When you run the script, you can:

    • Store a password for an account by entering the account name and the password.

    • Retrieve a password by entering the account name.

    • Exit the program.

Security Notes:

  • This is a basic password manager for educational purposes. While it uses encryption to secure passwords, there are many additional security measures to consider in a production system, like two-factor authentication and password strength checks.

  • Always store encryption keys securely. In a real application, you’d want to store the key in a more secure location, not as a plaintext file on the system.

This script provides a simple, secure way to manage passwords locally on your system using Python.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About