Security Solutions
A step-by-step guide to creating a secure Python password manager
Start LearningYou control the encryption and storage, eliminating third-party risks.
Tailor features to your specific needs and workflow.
Gain practical skills in Python, encryption, and security best practices.
Before we begin coding, you'll need Python installed on your Windows machine.
python --version
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.
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()
Now that you've written the code, let's save and run it:
password_manager.py
python password_manager.py
Make sure to keep your encryption key file safe - without it, you won't be able to decrypt your passwords!
Here's how to use your new password manager:
Security Tip: Never share your key file or store it with your password file. Consider keeping it on a separate USB drive.
For portability and security, you can run your password manager from a USB drive using a portable Python version:
E:
)E:\PasswordManager
)E:\WinPython
)password_manager.py
file to the E:\PasswordManager
folderWinPython Command Prompt.exe
cd \PasswordManager
)cryptography
library with pip install cryptography
python password_manager.py
Security Tip: Always eject the USB drive properly to prevent data corruption, and consider encrypting the USB drive itself for extra security.
Use Tkinter or PyQt to create a graphical interface for your password manager.
Add functionality to securely backup your passwords to cloud storage.
Implement a strong password generator with customizable options.