Sample code for 30+ languages & platforms
Android™

Get the Certificate Used for Decryption

See more MIME Examples

Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.

After a successful decryption, LastDecryptCert loads the certificate(s) actually used into a Cert object, indexed from zero up to NumDecryptCerts minus one.

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. Following decryption or security unwrapping, Chilkat records which certificate(s) were used. LastDecryptCert retrieves them so the application can report or verify the identity involved.

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;

    CkMime mime = new CkMime();
    success = mime.LoadMimeFile("qa_data/encrypted.eml");
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    //  Configure a decryption source and decrypt.
    String pfxPassword = "myPfxPassword";
    success = mime.AddPfxSourceFile("qa_data/recipient.pfx",pfxPassword);
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    success = mime.Decrypt();
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    //  After a successful decryption, load the certificate(s) that were used.  The 1st argument is the
    //  zero-based index and the 2nd is a Cert that receives the certificate.
    int n = mime.get_NumDecryptCerts();
    CkCert cert = new CkCert();
    int i;
    for (i = 0; i <= n - 1; i++) {
        success = mime.LastDecryptCert(i,cert);
        if (success == false) {
            Log.i(TAG, mime.lastErrorText());
            return;
            }

        Log.i(TAG, "Decrypted with: " + cert.subjectCN());
        }


  }

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