Sample code for 30+ languages & platforms
Android™

Use RSAES-OAEP Padding for Encrypted Email

See more Email Object Examples

Demonstrates the Chilkat Email.OaepPadding property, which selects the RSA encryption scheme used when encrypting email. The default is false, which selects RSAES PKCS1-v1_5; setting it to true selects RSAES-OAEP. This affects the RSA key-encryption step used for S/MIME encryption — it does not change the symmetric content-encryption algorithm chosen by Pkcs7CryptAlg. This example enables OAEP padding.

Background: Recall that S/MIME encrypts the message body with a one-time symmetric key, then encrypts that key with RSA. "Padding" is how the key bytes are formatted before RSA is applied. The older PKCS1-v1_5 padding has known theoretical weaknesses, while RSAES-OAEP adds randomness and a hash-based construction that is provably more secure and is the modern recommendation. Both endpoints must agree, so a recipient's software has to support OAEP to read the message.

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.OaepPadding property, which selects the RSA encryption scheme
    //  used when encrypting email.  The default is false (RSAES PKCS1-v1_5).  Set it to true
    //  to use RSAES-OAEP.  This affects only the RSA key-encryption step, not the symmetric
    //  content-encryption algorithm selected by Pkcs7CryptAlg.

    CkEmail email = new CkEmail();
    email.put_Subject("Encrypted with RSAES-OAEP");
    email.put_Body("This message uses OAEP padding for the RSA key encryption.");

    //  Use RSAES-OAEP instead of the older PKCS1-v1_5 scheme.
    email.put_OaepPadding(true);

    Log.i(TAG, "OaepPadding = " + String.valueOf(email.get_OaepPadding()));

  }

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