Sample code for 30+ languages & platforms
Android™

Select the S/MIME Encryption Algorithm

See more Email Object Examples

Demonstrates the Chilkat Email.Pkcs7CryptAlg property, which selects the underlying symmetric encryption algorithm used when an email is sent encrypted with PKCS#7 public-key (S/MIME) encryption. Possible values are aes, aes-gcm, des, 3des, and rc2; the default is aes. This example selects AES and pairs it with a 256-bit key length.

Background: S/MIME encryption is hybrid. The message body is encrypted with a fast symmetric algorithm (the one this property chooses, such as AES) using a randomly generated one-time key; then that one-time key is itself encrypted with each recipient's public key. This gives the speed of symmetric encryption with the key-distribution convenience of public-key cryptography. Modern choices like aes are strongly preferred — older options such as des and rc2 are considered weak.

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);

    //  Demonstrates the Email.Pkcs7CryptAlg property, which selects the symmetric encryption
    //  algorithm used when an email is sent encrypted with PKCS#7 (S/MIME) public-key
    //  encryption.  Possible values: aes, aes-gcm, des, 3des, rc2.  The default is aes.

    CkEmail email = new CkEmail();
    email.put_Subject("Encrypted email");
    email.put_Body("This message will be S/MIME encrypted when sent.");

    //  Select AES with a 256-bit key for the symmetric encryption.
    email.put_Pkcs7CryptAlg("aes");
    email.put_Pkcs7KeyLength(256);

    Log.i(TAG, "Pkcs7CryptAlg = " + email.pkcs7CryptAlg());
    Log.i(TAG, "Pkcs7KeyLength = " + String.valueOf(email.get_Pkcs7KeyLength()));

  }

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