Android™
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkMime mime = new CkMime();
success = mime.SetBodyFromPlainText("This message will be encrypted.");
if (success == false) {
Log.i(TAG, mime.lastErrorText());
return;
}
// Add a certificate for each intended recipient. Every recipient receives an independently
// wrapped copy of the message key.
CkCert cert1 = new CkCert();
success = cert1.LoadFromFile("qa_data/recipient1.cer");
if (success == false) {
Log.i(TAG, cert1.lastErrorText());
return;
}
success = mime.AddEncryptCert(cert1);
if (success == false) {
Log.i(TAG, mime.lastErrorText());
return;
}
CkCert cert2 = new CkCert();
success = cert2.LoadFromFile("qa_data/recipient2.cer");
if (success == false) {
Log.i(TAG, cert2.lastErrorText());
return;
}
success = mime.AddEncryptCert(cert2);
if (success == false) {
Log.i(TAG, mime.lastErrorText());
return;
}
// Encrypt for every added recipient at once.
success = mime.EncryptN();
if (success == false) {
Log.i(TAG, mime.lastErrorText());
return;
}
success = mime.SaveMime("qa_output/encrypted_multi.eml");
if (success == false) {
Log.i(TAG, mime.lastErrorText());
return;
}
Log.i(TAG, "Message encrypted for multiple recipients.");
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}