Get a Pentest and security assessment of your IT network.

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:

  • Stanford Javascript Crypto Library (SJCL): A well-established library with a wide range of algorithms.
  • WebCrypto API: Native browser API offering strong cryptography, but requires more code and handling for compatibility.
  • Forge: Another robust option supporting various cryptographic functions.

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:

  • Browser LocalStorage: Simple but less secure. Data can be accessed by other scripts on the same domain.
    localStorage.setItem('encryptionKey', secretKey);
    const storedKey = localStorage.getItem('encryptionKey');
  • Browser Session Storage: More secure than LocalStorage as data is cleared when the browser session ends.
  • IndexedDB: A more robust client-side database offering better security and storage capacity.
  • Web Authentication API (Passkeys): The most secure option. Uses platform authentication for key management, avoiding storing keys directly in the browser.

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

6. Important Considerations

  • Key Rotation: Regularly change encryption keys to minimize the impact of a potential compromise.
  • Algorithm Choice: AES is a widely used and secure algorithm, but stay updated on best practices.
  • Error Handling: Implement robust error handling for encryption/decryption failures.
  • Data Integrity: Consider adding a message authentication code (MAC) to verify data integrity.
  • cyber security Audit: Regularly audit your implementation for vulnerabilities.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation