Sample code for 30+ languages & platforms
Android™

Set the OAEP MGF Hash for Encrypted Email

See more Email Object Examples

Demonstrates the Chilkat Email.OaepMgfHash property, which selects the mask-generation function (MGF) hash algorithm used by OAEP padding when encrypting email with RSAES-OAEP. Valid values are sha1, sha256, sha384, and sha512; the default is sha256. It is used only when OaepPadding is true, and some recipients require the MGF hash to match OaepHash.

Background: OAEP internally needs a "mask generation function" (MGF) — a way to stretch a hash into a longer pseudo-random mask — and that MGF is itself built on a hash algorithm. In principle the MGF hash can differ from the main OAEP hash, but many implementations assume the two are the same, so setting both to sha256 is the safest interoperable choice. Mismatched settings between sender and recipient are a common cause of "decryption failed" errors.

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.OaepMgfHash property, which selects the mask-generation
    //  function (MGF) hash algorithm used by OAEP padding when encrypting with RSAES-OAEP.
    //  Valid values: sha1, sha256, sha384, sha512.  The default is sha256.  It is used only
    //  when OaepPadding is true.

    CkEmail email = new CkEmail();
    email.put_Subject("Encrypted with RSAES-OAEP");

    email.put_OaepPadding(true);
    email.put_OaepHash("sha256");

    //  Some recipients require the MGF hash to match the OAEP hash.
    email.put_OaepMgfHash("sha256");

    Log.i(TAG, "OaepMgfHash = " + email.oaepMgfHash());

  }

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