Data encryption
Encrypt data anonymously

Encryption of anonymous data

What is encryption of anonymous data ?

The encryption of anonymous data involves securing data that has been stripped of personally identifiable information (PII) to prevent unauthorized access, while maintaining anonymity. This practice ensures that even if the data is intercepted or accessed by unauthorized parties, the content remains unreadable and the individuals or entities involved cannot be identified.

Encrypt a form anonymously with Secrecy SDK

The encryptAnonymous method from the Secrecy library ensures that sensitive form data is securely encrypted using the user’s public key before being transmitted or stored. It first checks for the availability of the secrecy client, get the user id calling the secrecyClient.me method. Then retrieves the public key with the secrecy secrecyClient.app.userPublicKey method, converts the form data into a format suitable for encryption, and finally encrypts it using the encryptAnonymous method. The function provides robust protection for sensitive information in user forms.

formAnonymous.ts
import { encryptAnonymous } from '@secrecy/lib';
 
const encryptAnonymouslyFormData = async (
  formValues: any
): Promise<Uint8Array | null> => {
  // First we need to check if the secrecyClient is available
  if (!secrecyClient) {
    return null;
  }
 
  // Second we need to get the user public key
  const me = await secrecyClient.me();
  const userPublicKey = await secrecyClient.app.userPublicKey(me.id);
 
  // Third we need to encrypt the form values
  const encoder = new TextEncoder();
  const encoded = encoder.encode(JSON.stringify(formValues));
  const encrypted = encryptAnonymous(encoded, userPublicKey);
 
  // Finally we can return the encrypted form values and store it in the database
  return encrypted;
};

Decrypt the encrypted data of an anomime form with Secrecy SDK

The decryptAnonymous function securely handles the process of decrypting encrypted data using a SecrecyClient, decoding the raw bytes into a readable string, and converting that string back into its original form (most likely a JSON object). It's useful when you need to retrieve and interpret encrypted information in its original structure.

decryptedAnonymousData.ts
const decryptAnonymousFormData = ({ data }: { data: Uint8Array }) => {
  // First we need to check if the secrecyClient is available
  if (!secrecyClient) {
    return null;
  }
 
  // Second we need to decrypt the form values
  const decrypted = secrecyClient.decryptAnonymous(data);
  const decoder = new TextDecoder();
  const decoded = decoder.decode(decrypted);
 
  // Finally we can return the decrypted form values
  const formValues = JSON.parse(decoded);
  return formValues;
};