Sample code for 30+ languages & platforms
Android™

MIME S/MIME Signing Algorithm Properties

See more MIME Examples

Demonstrates the properties that control S/MIME signing: SigningAlg (the RSA signature scheme, PKCS1-V1_5 or RSASSA-PSS), SigningHashAlg (the message-digest algorithm), Micalg and Protocol (the micalg and protocol parameters of a multipart/signed Content-Type), and UseXPkcs7 (whether historical x-pkcs7 media-type names are used). The properties are configured before creating a detached signature.

Background. These settings determine how the CMS/PKCS #7 signature is produced and how a multipart/signed wrapper advertises it. The defaults (PKCS1-V1_5, sha256) suit most modern uses; the properties allow interoperability with specific requirements.

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 signed.");
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    //  SigningAlg is the RSA signature scheme.  The default is PKCS1-V1_5; set RSASSA-PSS (or pss) for
    //  RSASSA-PSS.
    mime.put_SigningAlg("PKCS1-V1_5");

    //  SigningHashAlg is the message-digest algorithm.  The default is sha256.
    mime.put_SigningHashAlg("sha256");

    //  Micalg and Protocol are the micalg and protocol parameters written into a multipart/signed
    //  Content-Type header field.  They are normally set automatically, but can be controlled here.
    mime.put_Micalg("sha-256");
    mime.put_Protocol("application/pkcs7-signature");

    //  UseXPkcs7 controls whether newly created S/MIME media types use the historical x-pkcs7 names.
    mime.put_UseXPkcs7(false);

    //  Load a signing certificate (with private key access) from a PFX.  The PFX password should come
    //  from a secure source rather than being hard-coded.
    String pfxPassword = "myPfxPassword";
    CkCert cert = new CkCert();
    success = cert.LoadPfxFile("qa_data/signer.pfx",pfxPassword);
    if (success == false) {
        Log.i(TAG, cert.lastErrorText());
        return;
        }

    //  Create the detached signature using the algorithms configured above.
    success = mime.AddDetachedSignature(cert);
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    Log.i(TAG, "Signed with SigningAlg=" + mime.signingAlg() + " SigningHashAlg=" + mime.signingHashAlg());

  }

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