Blog | G5 Cyber Security

Secure User Private Key Management

TL;DR

Don’t store user private keys directly on your servers! Use a combination of client-side encryption, secure key generation within the application, and potentially hardware security modules (HSMs) or trusted execution environments (TEEs) for advanced protection. Focus on letting users control their keys as much as possible.

1. Understand the Risks

Storing private keys is extremely sensitive. If compromised, attackers can impersonate your users and access their data/funds. Common risks include:

2. Key Generation – User Control is Best

The safest approach is for users to generate their own keys, ideally on their devices.

Example (JavaScript using WebCrypto API):

const key = window.crypto.subtle.generateKey(
  {
    name: "RSA-OAEP",
    modulusLength: 2048,
    publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
    hash: { name: "SHA-256" },
  },
  true,
  ["encrypt", "decrypt"]
);

3. Client-Side Encryption

If you *must* store any data related to the private key (e.g., encrypted metadata), encrypt it using a key derived from the user’s password or passphrase.

Example (JavaScript using CryptoJS):

const encrypted = CryptoJS.AES.encrypt('sensitive data', 'user_password').toString();

4. Secure Storage Options

  1. Local Storage (with caution): If you use local storage, encrypt the key before storing it. Be aware that local storage is vulnerable to XSS attacks.
  2. IndexedDB: Offers more security features than local storage but still requires encryption.
  3. Platform Keychains/Keystores: The most secure option for mobile apps – keys are stored in a dedicated, protected hardware environment.

5. Server-Side Considerations (Minimize Storage)

Avoid storing private keys directly on your servers whenever possible.

6. Key Rotation

Regularly rotate user keys to limit the impact of potential compromises.

7. Multi-Factor Authentication (MFA)

Even with secure key management, MFA adds an extra layer of protection against unauthorized access.

8. Regular Security Audits

Conduct regular security audits and penetration testing to identify and address vulnerabilities in your key management system.

Exit mobile version