Android™
Android™
Set Cert and Private Key for S/MIME Decryption
See more IMAP Examples
Demonstrates the Chilkat Imap.SetDecryptCert2 method, which specifies a certificate together with a separately supplied private key for decrypting S/MIME messages. The first argument is the Cert and the second is the PrivateKey. This example loads the certificate and key from separate files.
Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.
Background: Sometimes the certificate (the public part) and the private key are stored separately — a
.cer alongside a PEM key file, for instance. This method pairs them explicitly, whereas SetDecryptCert expects a single Cert that already provides its private key. After the pairing, subsequently downloaded S/MIME messages are decrypted automatically.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 Imap.SetDecryptCert2 method, which specifies a certificate together with a
// separately supplied private key for decrypting S/MIME messages. The 1st argument is the
// Cert and the 2nd is the PrivateKey.
CkImap imap = new CkImap();
imap.put_Ssl(true);
imap.put_Port(993);
success = imap.Connect("imap.example.com");
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
success = imap.Login("user@example.com","myPassword");
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
// Load the certificate and its private key from separate files.
CkCert cert = new CkCert();
success = cert.LoadFromFile("qa_data/smime.cer");
if (success == false) {
Log.i(TAG, cert.lastErrorText());
return;
}
CkPrivateKey privKey = new CkPrivateKey();
success = privKey.LoadPemFile("qa_data/smime_privkey.pem");
if (success == false) {
Log.i(TAG, privKey.lastErrorText());
return;
}
// Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
success = imap.SetDecryptCert2(cert,privKey);
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
success = imap.Disconnect();
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
}
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."
}
}