Android™ Requires Chilkat v11.0.0+
Android™
Sign a saved MIME message with DKIM
See more DKIM Examples
Sign the complete bytes of message.eml and save the result as signed.eml. Finish the message’s From, To, Date, Message-ID, MIME parts and transfer encodings before signing. Existing signatures stay in place and the new signature is inserted first.
The example signs the full body and includes From in the signed headers. Relaxed canonicalization tolerates defined whitespace and header folding changes. Editing a signed field or changing the body text can still break verification. Prepare the matching DNS record.
Chilkat Android™ Downloads
// 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;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkBinData message = new CkBinData();
success = message.LoadFile("message.eml");
if (success != true) {
Log.i(TAG, "Could not read message.eml.");
return;
}
// Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
CkPrivateKey privateKey = new CkPrivateKey();
success = privateKey.LoadAnyFormatFile("dkim-private.pem","");
if (success != true) {
Log.i(TAG, privateKey.lastErrorText());
return;
}
CkDkim signer = new CkDkim();
signer.put_DkimDomain("example.com");
signer.put_DkimSelector("mail2026");
signer.put_DkimAlg("rsa-sha256");
success = signer.SetDkimPrivateKey(privateKey);
if (success != true) {
Log.i(TAG, signer.lastErrorText());
return;
}
// DkimCanon takes a single setting and applies it to both headers and body.
signer.put_DkimCanon("relaxed");
signer.put_DkimHeaders("from:to:subject:date:message-id:mime-version:content-type:content-transfer-encoding");
// Zero signs the entire canonicalized body and omits the optional l= tag.
signer.put_DkimBodyLengthCount(0);
success = signer.DkimSign(message);
if (success != true) {
Log.i(TAG, signer.lastErrorText());
return;
}
success = message.WriteFile("signed.eml");
if (success != true) {
Log.i(TAG, "Could not write signed.eml.");
return;
}
Log.i(TAG, "Saved signed.eml.");
}
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."
}
}