Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.8.0+

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s certificate and private key (from a PEM file) before opening signed-encrypted.eml. This workflow expects both encryption and a signature, so it checks both layers before reading the message. Cryptographic signature validity is one input to a signer-trust decision.

Chilkat Android™ Downloads

Android™
// 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;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkEmail email = new CkEmail();
    //  The recipient's certificate and private key are in a PEM file.  The 2nd argument of LoadPemFile
    //  is the password of the private key.
    CkPem pem = new CkPem();
    success = pem.LoadPemFile("recipient.pem","RECIPIENT_PASSWORD");
    if (success != true) {
        Log.i(TAG, pem.lastErrorText());
        return;
        }

    success = email.AddPemSource(pem);
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    success = email.LoadEml("signed-encrypted.eml");
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    if ((email.get_ReceivedEncrypted() != true) or (email.get_Decrypted() != true)) {
        Log.i(TAG, "Expected a successfully decrypted S/MIME envelope.");
        return;
        }

    if ((email.get_ReceivedSigned() != true) or (email.get_SignaturesValid() != true)) {
        Log.i(TAG, "Expected a valid signature inside the envelope.");
        return;
        }

    //  This message has one signer. Examine its certificate after verification.
    CkCert signer = new CkCert();
    success = email.LastSignerCert(0,signer);
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    Log.i(TAG, "Signer certificate: " + signer.subjectDN());
    Log.i(TAG, "Recovered subject: " + email.subject());
    Log.i(TAG, "Recovered attachments: " + String.valueOf(email.get_NumAttachments()));

  }

  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."
  }
}