Blog | G5 Cyber Security

Client-Side Encryption Key Storage

TL;DR

Storing encryption keys securely on the client-side is very difficult. The best approach involves a combination of techniques: using a strong key derivation function (KDF) with user authentication, encrypting the key itself with a password derived from the KDF, and storing it in browser storage (Local Storage or IndexedDB). Never store keys directly! Always consider the risks and limitations.

1. Understand the Risks

Client-side encryption means the decryption happens entirely within the user’s browser. This is great for privacy, but also presents significant security challenges:

Because of these risks, client-side encryption is best suited for protecting data from casual observers, not highly determined attackers.

2. Key Derivation Function (KDF)

Don’t use the user’s password directly as the encryption key! Use a KDF to create a strong, unique key from the password. PBKDF2 and Argon2 are good choices.

// Example using PBKDF2 (JavaScript - requires a library like crypto-js)

3. Encrypting the Key

Once you have a key derived from the password, use it to encrypt the actual encryption key itself.

// Example using CryptoJS to encrypt the key

4. Secure Storage in the Browser

Choose a secure browser storage option:

// Example storing encrypted key in Local Storage

5. Implementing Authentication

Require user authentication before accessing the encryption key.

Always re-prompt for the password when decrypting data.

6. Code Example (Conceptual)

// Simplified example - DO NOT use in production without proper security review!

7. Important Considerations

Exit mobile version