Sample code for 30+ languages & platforms
Android™

Create PKCS7 (CMS) EnvelopedData

Encrypt some data to a recipient by creating a PKCS7 (CMS) EnvelopedData structure. The data will be encrypted using a symmetric content-encryption algorithm (e.g., AES), and the randomly generated symmetric key will be encrypted using the recipient’s RSA public key extracted from their X.509 certificate.

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;

    CkCrypt2 crypt = new CkCrypt2();

    //  Specify the encryption to be used.
    //  "pki" indicates "Public Key Infrastructure" and will create a PKCS7 encrypted (enveloped-data) message.
    crypt.put_CryptAlgorithm("pki");
    crypt.put_Pkcs7CryptAlg("aes");
    crypt.put_KeyLength(256);
    crypt.put_OaepHash("sha256");
    crypt.put_OaepPadding(true);

    CkCert cert = new CkCert();

    //  Use a certificate found in the Windows certificate store.
    success = cert.LoadByCommonName("My Certificate");
    if (success != true) {
        Log.i(TAG, cert.lastErrorText());
        return;
        }

    //  Tell the crypt object to use the certificate.
    crypt.SetEncryptCert(cert);

    String toBeEncrypted = "This string is to be encrypted.";

    //  Get the result in multi-line BASE64 MIME format.
    crypt.put_EncodingMode("base64_mime");
    crypt.put_Charset("utf-8");

    String result = crypt.encryptStringENC(toBeEncrypted);
    if (success != true) {
        Log.i(TAG, crypt.lastErrorText());
        return;
        }

    //  -------------------------------------------------------------------------
    //  See the following example to decrypt what was created in this example
    //  Decrypt PKCS7 (CMS) EnvelopedData
    //  -------------------------------------------------------------------------

    Log.i(TAG, result);

    //  Sample output:

    //  MIICSgYJKoZIhvcNAQcDoIICOzCCAjcCAQAxggHiMIIB3gIBADCBljCBgTELMAkGA1UEBhMCSVQx
    //  EDAOBgNVBAgMB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8xFzAVBgNVBAoMDkFj
    //  dGFsaXMgUy5wLkEuMSwwKgYDVQQDDCNBY3RhbGlzIENsaWVudCBBdXRoZW50aWNhdGlvbiBDQSBH
    //  MwIQPCWvkSv8oQ7xRmEHJ6TzEDA8BgkqhkiG9w0BAQcwL6APMA0GCWCGSAFlAwQCAQUAoRwwGgYJ
    //  KoZIhvcNAQEIMA0GCWCGSAFlAwQCAQUABIIBAKqHAPQNSsQoX7B2NH7QyEOWQRsSVs8oCHXmy8f4
    //  MVZD2er3bvYUCIomxpwbLEAl14qjUIMynahooYGgqip7+4FqL301G+BVjZVfEhHWj+VI1dAWnWuL
    //  VHlvc/pbQNBWqV8rKVJsNIsuAZkdj4WSwLVKxYkYX43B8fh/g71XN2DTJu7Z/824v48KBmgpQBOT
    //  2q7IcDGxNPAFN2p6eavIVGn2LvhEbf/Fszyj+GR5tMcnQP1BOLJ3s3JzUBbvj8hcZrF1Vhl9HnTU
    //  YQx8G/KdW1mR+Wlhl3BWoK0LYKRTbnTx2BXOs0CY1SXOAdhKr01ZYjA+xW4nGzY0lfXS9QZjh9gw
    //  TAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBKgQQw0xTbfmnt0zjWHo5SaQIp4AgxTVY9E/Ncqy6t+RM
    //  8y4c3Av62/wB8IpPUEmtM2OeuZo=

  }

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