Sample code for 30+ languages & platforms
React Native

Encrypt MIME for Multiple Recipients

See more MIME Examples

Demonstrates encrypting the current MIME entity for several recipients at once. A certificate is added for each recipient with AddEncryptCert, and EncryptN then produces a single CMS/PKCS #7 message that each recipient can independently decrypt.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. When a message targets more than one recipient, each recipient's certificate is added to the internal list and EncryptN wraps the message key separately for each one. Any single recipient can then decrypt with their own private key.

Chilkat React Native Downloads

React Native
import { Cert, Mime } from '@chilkat/react-native'

function chilkatExample() {
  const mime = new Mime();
  try {
    mime.setBodyFromPlainText('This message will be encrypted.');
  } catch {
    console.log(mime.lastErrorText);
    return;
  }

  // Add a certificate for each intended recipient.  Every recipient receives an independently
  // wrapped copy of the message key.
  const cert1 = new Cert();
  try {
    cert1.loadFromFile('qa_data/recipient1.cer');
  } catch {
    console.log(cert1.lastErrorText);
    return;
  }

  try {
    mime.addEncryptCert(cert1);
  } catch {
    console.log(mime.lastErrorText);
    return;
  }

  const cert2 = new Cert();
  try {
    cert2.loadFromFile('qa_data/recipient2.cer');
  } catch {
    console.log(cert2.lastErrorText);
    return;
  }

  try {
    mime.addEncryptCert(cert2);
  } catch {
    console.log(mime.lastErrorText);
    return;
  }

  // Encrypt for every added recipient at once.
  try {
    mime.encryptN();
  } catch {
    console.log(mime.lastErrorText);
    return;
  }

  try {
    mime.saveMime('qa_output/encrypted_multi.eml');
  } catch {
    console.log(mime.lastErrorText);
    return;
  }

  console.log('Message encrypted for multiple recipients.');
}