Android™
Android™
Set the Decryption Certificate and Private Key
See more Email Object Examples
Demonstrates the Chilkat Email.SetDecryptCert2 method, which explicitly provides a certificate and its corresponding private key as separate objects for decrypting a received encrypted email. Set them before loading the encrypted message. This example loads a .cer certificate and a PEM private key, sets both, then loads an encrypted email.
Background: Sometimes the certificate and its private key are stored separately — a public
.cer file plus a PEM (or other format) key file — rather than combined in a PFX. SetDecryptCert2 accepts the two objects individually, which is the natural fit for that arrangement. It is the two-object counterpart to SetDecryptCert, which takes a single certificate that already carries its 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;
// Demonstrates the SetDecryptCert2 method, which explicitly provides a certificate and its
// corresponding private key (as separate objects) for decrypting a received encrypted
// email. Set them before loading the encrypted email.
CkEmail email = new CkEmail();
// Load the certificate (public) and the matching private key from separate files.
CkCert cert = new CkCert();
success = cert.LoadFromFile("qa_data/certs/recipient.cer");
if (success == false) {
Log.i(TAG, cert.lastErrorText());
return true;
}
CkPrivateKey privKey = new CkPrivateKey();
success = privKey.LoadPemFile("qa_data/certs/recipient_privkey.pem");
if (success == false) {
Log.i(TAG, privKey.lastErrorText());
return true;
}
// Provide the certificate and private key to use for decryption.
success = email.SetDecryptCert2(cert,privKey);
if (success == false) {
Log.i(TAG, email.lastErrorText());
return true;
}
// Load the encrypted email; Chilkat decrypts it using the certificate and key.
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."
}
}