Sample code for 30+ languages & platforms
Android™

MIME S/MIME Encryption Algorithm Properties

See more MIME Examples

Demonstrates the properties that control S/MIME encryption: Pkcs7CryptAlg (the symmetric content-encryption algorithm), Pkcs7KeyLength (the content-encryption key length in bits), OaepPadding (whether RSAES-OAEP key transport is used instead of RSAES-PKCS1-v1_5), and the OAEP hash settings OaepHash and OaepMgfHash. The properties are configured before calling Encrypt.

Background. CMS/PKCS #7 encryption uses a symmetric algorithm to protect the content and an RSA key-transport scheme to protect the symmetric key. The defaults (aes, 128-bit, PKCS1-v1_5 padding) work broadly; these properties tune the algorithms and padding.

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.SetBodyFromPlainText("This message will be encrypted.");
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    //  Pkcs7CryptAlg is the symmetric content-encryption algorithm.  Supported values are aes, aes-gcm,
    //  des, 3des, and rc2.  The default is aes.
    mime.put_Pkcs7CryptAlg("aes");

    //  Pkcs7KeyLength is the content-encryption key length in bits.  The default is 128.
    mime.put_Pkcs7KeyLength(256);

    //  OaepPadding selects the RSA key-transport padding.  The default is false (RSAES-PKCS1-v1_5).
    //  Set true to use RSAES-OAEP, in which case the OAEP hash settings apply.
    mime.put_OaepPadding(true);
    mime.put_OaepHash("sha256");
    mime.put_OaepMgfHash("sha256");

    //  Load the recipient certificate (public key is sufficient for encrypting).
    CkCert cert = new CkCert();
    success = cert.LoadFromFile("qa_data/recipient.cer");
    if (success == false) {
        Log.i(TAG, cert.lastErrorText());
        return;
        }

    //  Encrypt using the algorithm settings configured above.
    success = mime.Encrypt(cert);
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    Log.i(TAG, "Encrypted with " + mime.pkcs7CryptAlg() + " " + String.valueOf(mime.get_Pkcs7KeyLength()) + "-bit key.");

  }

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