Assume I'm in python and I have a password input in a variable 'my_pass', how would I save it locally in an encrypted way so I could load it back when needed.
Make sure the user is the only one who can decrypt it by using some private key or similar solution.
Assume I'm running from a linux based OS.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To securely save a password locally in an encrypted format on a Linux-based system using Python, you can use the cryptography library along with a user-specific private key. Below is a step-by-step solution:
Steps:
Generate a private/public key pair (once per user).
Encrypt the password using the public key.
Save the encrypted password to a file.
Decrypt the password when needed using the private key.
Code Implementation:
python
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
import os
# Step 1: Generate or load RSA keys (run once per user)
def generate_or_load_keys(key_dir="~/.password_keys"):
key_dir = os.path.expanduser(key_dir)
private_key_path = os.path.join(key_dir, "private_key.pem")
public_key_path = os.path.join(key_dir, "public_key.pem")
if not os.path.exists(key_dir):
os.makedirs(key_dir)
if not os.path.exists(private_key_path):
# Generate new keys
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=4096,
)
public_key = private_key.public_key()
# Save keys
with open(private_key_path, "wb") as f:
f.write(private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption() # No passphrase for simplicity
))
with open(public_key_path, "wb") as f:
f.write(public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
))
else:
# Load existing keys
with open(private_key_path, "rb") as f:
private_key = serialization.load_pem_private_key(
f.read(),
password=None,
)
with open(public_key_path, "rb") as f:
public_key = serialization.load_pem_public_key(f.read())
return private_key, public_key
# Step 2: Encrypt and save the password
def save_encrypted_password(password, public_key, encrypted_file="~/.encrypted_password"):
encrypted_file = os.path.expanduser(encrypted_file)
encrypted_password = public_key.encrypt(
password.encode(),
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
with open(encrypted_file, "wb") as f:
f.write(encrypted_password)
# Step 3: Load and decrypt the password
def load_decrypted_password(private_key, encrypted_file="~/.encrypted_password"):
encrypted_file = os.path.expanduser(encrypted_file)
with open(encrypted_file, "rb") as f:
encrypted_password = f.read()
decrypted_password = private_key.decrypt(
encrypted_password,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
).decode()
return decrypted_password
# Example usage:
if __name__ == "__main__":
my_pass = "my_super_secure_password"
# Generate/load keys (run once per user)
private_key, public_key = generate_or_load_keys()
# Save encrypted password
save_encrypted_password(my_pass, public_key)
# Load decrypted password
decrypted_pass = load_decrypted_password(private_key)
print("Decrypted Password:", decrypted_pass)
Key Points:
RSA Encryption: Uses public-key cryptography where only the private key (user-specific) can decrypt the data.
Key Storage: Private keys are saved locally in ~/.password_keys/ and should be protected.
Security: The private key is not encrypted with a passphrase for simplicity, but you can add one using serialization.BestAvailableEncryption for extra security.
Dependencies: Install the cryptography library:
bash
pip install cryptography
This ensures only the user with the private key can decrypt the password.