Blog | G5 Cyber Security

Client-Side Encryption in SaaS

TL;DR

Encrypt sensitive client data directly in their browser before it reaches your servers. This uses JavaScript libraries to handle the encryption, giving clients more control and improving security. We’ll cover key generation, encryption/decryption, and secure storage options.

1. Choose an Encryption Library

Several good JavaScript libraries are available for client-side encryption. Some popular choices include:

For this guide, we’ll use SJCL due to its ease of use.

2. Key Generation

The encryption key is crucial. Never store keys on your server! Generate the key in the client’s browser and let them manage it (e.g., through a password they create).

  1. Random Key: Generate a strong random key using SJCL.
    const secretKey = sjcl.randomWords(16); // Generates a 128-bit key
  2. Password-Based Key Derivation (PBKDF): Derive a key from the user’s password using PBKDF. This is more secure than storing the password directly.
    const salt = sjcl.randomWords(8); // Generate a random salt
    const secretKey = sjcl.pbkdf2("password", salt, 1000, 16);
    

Important: Always use a strong salt and sufficient iterations (e.g., 1000 or more) for PBKDF.

3. Encryption

Encrypt the data before sending it to your server.

  1. Convert Data: Convert the data you want to encrypt into a string.
  2. Encrypt with SJCL: Use the generated key to encrypt the data.
    const ciphertext = sjcl.encrypt("aes", secretKey, "your sensitive data here");

4. Decryption

Decrypt the data after receiving it from your server (in the client’s browser).

  1. Receive Ciphertext: Get the encrypted data from your server.
  2. Decrypt with SJCL: Use the same key to decrypt the ciphertext.
    const plaintext = sjcl.decrypt("aes", secretKey, ciphertext);

5. Secure Storage Options

How you store the encryption key is vital. Here are some options:

Recommendation: Avoid LocalStorage if possible. IndexedDB or Web Authentication API are preferred.

6. Important Considerations

Exit mobile version