Sample code for 30+ languages & platforms
Android™

Add a PFX Source from BinData

See more Email Object Examples

Demonstrates the Chilkat Email.AddPfxSourceBd method, which adds PFX data held in a BinData object to the list of certificate and private-key sources used during decryption or signing. The second argument is the PFX password. Call it before obtaining the encrypted email so Chilkat can decrypt it automatically. This example loads a PFX into a BinData, adds it, and then loads an encrypted email.

Background: This is the in-memory (BinData) counterpart to AddPfxSourceFile. It is the right choice when the PFX bytes come from somewhere other than a file — a database, a secrets manager, or an HTTP download — letting you supply the certificate and private key without first writing them to disk (which is preferable for sensitive key material).

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;

    //  Demonstrates the AddPfxSourceBd method, which adds PFX data (held in a BinData) to the
    //  list of certificate and private-key sources used during decryption or signing.  The
    //  second argument is the PFX password.  Call it before obtaining the encrypted email.

    CkEmail email = new CkEmail();

    //  Load PFX data into a BinData object.
    CkBinData bdPfx = new CkBinData();
    success = bdPfx.LoadFile("qa_data/certs/decryption.pfx");
    if (success == false) {
        Log.i(TAG, bdPfx.lastErrorText());
        return true;
        }

    //  Add the PFX as a source of the certificate + private key.
    success = email.AddPfxSourceBd(bdPfx,"pfx_password");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    //  Load an encrypted email; Chilkat decrypts it using the PFX source.
    success = email.LoadEml("qa_data/eml/encrypted.eml");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "Decrypted = " + String.valueOf(email.get_Decrypted()));

    //  Note: Paths such as "qa_data/..." are relative local filesystem paths,
    //  relative to the current working directory of the running application.

  }

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