Here's a Python solution using the cryptography library that provides secure encryption with a user-specific approach:
from cryptography.fernet import Fernet
import os
import getpass
def generate_key():
"""
Generate a key and save it to a file in the user's home directory
with restricted permissions
"""
key = Fernet.generate_key()
key_path = os.path.join(os.path.expanduser('~'), '.secret.key')
# Create key file with restricted permissions (only readable by the owner)
with os.open(key_path, os.O_WRONLY | os.O_CREAT, 0o600) as key_file:
os.write(key_file, key)
return key
def encrypt_password(password):
"""
Encrypt the password using Fernet symmetric encryption
"""
# Try to load existing key, or generate a new one
key_path = os.path.join(os.path.expanduser('~'), '.secret.key')
try:
with open(key_path, 'rb') as key_file:
key = key_file.read()
except FileNotFoundError:
key = generate_key()
# Create a Fernet instance
fernet = Fernet(key)
# Encrypt the password
encrypted_password = fernet.encrypt(password.encode())
return encrypted_password
def decrypt_password(encrypted_password):
"""
Decrypt the password using the same key
"""
key_path = os.path.join(os.path.expanduser('~'), '.secret.key')
# Read the key
with open(key_path, 'rb') as key_file:
key = key_file.read()
# Create a Fernet instance
fernet = Fernet(key)
# Decrypt the password
decrypted_password = fernet.decrypt(encrypted_password).decode()
return decrypted_password
# Example usage
def main():
# Get password securely
my_pass = getpass.getpass("Enter your password: ")
# Encrypt and save
encrypted_pass = encrypt_password(my_pass)
# Save encrypted password to a file (optional)
with open('encrypted_password.bin', 'wb') as f:
f.write(encrypted_pass)
# Later, to retrieve and decrypt
with open('encrypted_password.bin', 'rb') as f:
loaded_encrypted_pass = f.read()
# Decrypt
decrypted_pass = decrypt_password(loaded_encrypted_pass)
print("Decrypted password:", decrypted_pass)
# Key security features:
# 1. Key is stored in user's home directory
# 2. Key file permissions are set to 0600 (read/write for owner only)
# 3. Uses Fernet symmetric encryption
# 4. Uses getpass to securely input password
Key security considerations:
- Uses Fernet symmetric encryption from the
cryptography library
- Generates a unique encryption key stored in the user's home directory
- Sets strict file permissions so only the user can read the key
- Uses
getpass to securely input the password without echoing
- The key is specific to the user's system
Prerequisites:
pip install cryptography
Security notes:
- This method ties the encryption to the specific user's system
- The key is stored locally, making it accessible only to the user
- Fernet provides authenticated encryption (prevents tampering)
Potential improvements:
- Consider using a hardware security module (HSM) for even more secure key storage
- For very sensitive applications, consider using a password manager or keyring system
Limitations:
- If someone gains full system access, they could potentially extract the key
- Not suitable for multi-user scenarios without modification