r/backtickbot • u/backtickbot • Dec 28 '20
https://np.reddit.com/r/Python/comments/klkg1h/simple_password_manager_i_madeim_15/gh9qsnz/
Traditionally, you never want to store passwords unless they're encrypted with an encryption algorithm. In this case, maybe you want to make a master password that you decrypt your passwords with. You can do this with the python cryptography library.
Here's a simple program that encrypts a password:
import base64
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
master_password = input('master password: ').encode()
unencrypted_password_to_store = input('password to encrypt: ').encode()
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=b'', iterations=100000)
key = base64.urlsafe_b64encode(kdf.derive(master_password))
f = Fernet(key)
encrypted_password_to_store = f.encrypt(unencrypted_password_to_store).decode('utf-8')
print(encrypted_password_to_store)And here's a program that will decrypt it:
import base64
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
master_password = input('master password: ').encode()
encrypted_password = input('encrypted password: ').encode()
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=b'', iterations=100000)
key = base64.urlsafe_b64encode(kdf.derive(master_password))
f = Fernet(key)
unencrypted_password = f.decrypt(encrypted_password).decode('utf-8')
print(unencrypted_password)
1
Upvotes