OpenPGP signing in Android

This sample code demonstrates how to OpenPGP digitally sign a data file using DidiSoft OpenPGP Library for Android.

The code below assumes that the private key that will be used for signing and the data file are located in the Assets folder of the Android application.

The signed output file is stored in the private context path of the application.

package android.demo;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.TextView;
import com.didisoft.pgp.PGPException;
import com.didisoft.pgp.PGPLib;
public class SignStreamDemo extends Activity {
 public void onCreate(Bundle savedInstanceState) {
  TextView tv = new TextView(this);
  InputStream dataStream = null;
  InputStream keyStream = null;
  OutputStream outStream = null;
  try {
    AssetManager assets = this.getAssets();
    // load data and private key stream
    dataStream =;
    keyStream =;
    // specify output stream
    outStream = this.openFileOutput("OUTPUT.pgp", MODE_PRIVATE);
    // This is just a file name label that will be associated with the encrypted data
    String internalFileNameLabel = "INPUT.txt";
    pgp.signStream(dataStream, internalFileNameLabel, keyStream, private_key_password, outStream, true);
    tv.append("Sigining done.");
  } catch (Exception e) {
  } finally {
    // cleanup
    if (dataStream != null)
    if (keyStream != null)
    if (outStream != null)

A complete working example can be found in /Examples folder in the library distribution ZIP archive.