Sample code for 30+ languages & platforms
Android™

Create CMS (PKCS7) Detached MIME Signature

Demonstrates how to add a CMS detached signature to MIME.

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;

    //  This example assumes the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    CkMime mime = new CkMime();

    //  Load a certificate w/ private key for signing.
    CkCert cert = new CkCert();
    success = cert.LoadPfxFile("qa_data/pfx/ecc256_12345.p12","12345");
    if (success != true) {
        Log.i(TAG, cert.lastErrorText());
        return;
        }

    Log.i(TAG, "Cert: " + cert.subjectDN());

    //  Create a simple MIME message to be signed:
    mime.AddHeaderField("Subject","This is a test");
    mime.AddHeaderField("MyCustomHeader","abc123");
    mime.put_ContentType("text/plain");
    mime.SetBody("This is a plain-text body.");

    //  Examine the MIME prior to signing:
    Log.i(TAG, "--- MIME prior to signing ---");
    Log.i(TAG, mime.getMime());

    //  The MIME prior to signing looks like this:

    //  Subject: This is a test
    //  MyCustomHeader: abc123
    //  Content-Type: text/plain; format=flowed
    //  
    //  This is a plain-text body.

    //  Use SHA256 as the hash algorithm in the CMS signature.
    mime.put_SigningHashAlg("SHA256");

    //  Add a detached signature:
    success = mime.AddDetachedSignature(cert);
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    //  Examine the MIME with detached signature:
    Log.i(TAG, "--- MIME with detached signature ---");
    Log.i(TAG, mime.getMime());

    //  Content-Type: multipart/signed; boundary="----=_NextPart_6e3_c655_9641494d.b4f00501";
    //   protocol="application/x-pkcs7-signature";
    //   micalg=sha256
    //  
    //  ------=_NextPart_6e3_c655_9641494d.b4f00501
    //  Subject: This is a test
    //  MyCustomHeader: abc123
    //  Content-Type: text/plain; format=flowed
    //  
    //  This is a plain-text body.
    //  ------=_NextPart_6e3_c655_9641494d.b4f00501
    //  Content-Transfer-Encoding: base64
    //  Content-Type: application/x-pkcs7-signature; name="smime.p7s"
    //  Content-Disposition: attachment; filename="smime.p7s"
    //  
    //  MIID7AYJKoZIhvcNAQcCoIID3TCCA9kCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg
    //  ggJxMIICbTCCAhOgAwIBAgIJAJuS2kgOoyr+MAkGByqGSM49BAEwWzELMAkGA1UEBhMCQVUxEzAR
    //  BgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEUMBIG
    //  A1UEAxMLZWNjMjU2LXRlc3QwHhcNMTYwODIzMTU0OTM5WhcNNDQwMTA4MTU0OTM5WjBbMQswCQYD
    //  VQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQ
    //  dHkgTHRkMRQwEgYDVQQDEwtlY2MyNTYtdGVzdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABARy
    //  Hd86A7+qQ4DlIfKynZaFGdGLpkU3GlQwqaVD6GIJg3QIDhaWEksYtZ9OWjNHn9a6+i/P9o5/NrdI
    //  SP0VWDWjgcAwgb0wHQYDVR0OBBYEFGmd2DfK4c3VBTVn8ebKH2BTmRicMIGNBgNVHSMEgYUwgYKA
    //  FGmd2DfK4c3VBTVn8ebKH2BTmRicoV+kXTBbMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1T
    //  dGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMRQwEgYDVQQDEwtlY2MyNTYt
    //  dGVzdIIJAJuS2kgOoyr+MAwGA1UdEwQFMAMBAf8wCQYHKoZIzj0EAQNJADBGAiEAhuiwGy0lZKtK
    //  LA13Ffc8AMTK7NsePPSfs0clXlZUR78CIQDXsJXpYsJ7tlTeockrK+6099sG/gGFTRPiQYU8juyf
    //  LzGCAT8wggE7AgEBMGgwWzELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNV
    //  BAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEUMBIGA1UEAxMLZWNjMjU2LXRlc3QCCQCbktpI
    //  DqMq/jANBglghkgBZQMEAgEFAKBpMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcN
    //  AQkFMQ8XDTIyMDUwODE0MzM1NFowLwYJKoZIhvcNAQkEMSIEIMvNpYuymLvF68aFFe+l3NEQTdkB
    //  7T8U6/j+Y5FgDlIUMAoGCCqGSM49BAMCBEYwRAIgQ6migEn3Le9ciIVUP/qc2JZAJZWjpulJXY9Q
    //  IZJ3XK0CIGZwpLOgiv7ES8958hqT9a/q7aPc1xmkaZHtsat/k+Ki
    //  
    //  ------=_NextPart_6e3_c655_9641494d.b4f00501--

  }

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