#๐Ÿ”’ Where or how are encryption keys typically stored and if they are needed for decryption?

8 messages ยท Page 1 of 1 (latest)

unreal prism
#

Currently put every key in a data/keys directory. Though, this is not secure. How do I securely store them if they are needed for decryption?

KEY_DIR = "data/keys"

def load_key(user_id: str) -> bytes:
    os.makedirs(KEY_DIR, exist_ok=True)
    key_path = os.path.join(KEY_DIR, f"{user_id}.key")

    if not os.path.exists(key_path):
        key = Fernet.generate_key()
        with open(key_path, "wb") as f:
            f.write(key)
    else:
        with open(key_path, "rb") as f:
            key = f.read()
    return key

def get_fernet(user_id: str) -> Fernet:
    return Fernet(load_key(user_id))

def encrypt_password(password: str, user_id: str) -> str:
    fernet = get_fernet(user_id)
    return fernet.encrypt(password.encode()).decode()

def decrypt_password(encrypted_password: str, user_id: str) -> str:
    fernet = get_fernet(user_id)
    return fernet.decrypt(encrypted_password.encode()).decode()
oblique yokeBOT
#

@unreal prism

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

junior pilot
#

Any chance you can ask the user for the key to decrypt the encryption/decryption key?

unreal prism
#

so a password protected file

#

so in order to decrypt ask the user for a password

junior pilot
#

If you have multiple key, yes, the safest way is ask the user for the master password to decrypt all the key

oblique yokeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.