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 java.io.*;
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 {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(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 = assets.open(INPUT_txt);
    keyStream = assets.open(private_key);
 
    // 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) {
    tv.append(e.getMessage());
  } finally {
    // cleanup
    if (dataStream != null)
	dataStream.close();
    if (keyStream != null)
        keyStream.close();
    if (outStream != null)
	outStream.close();
    }
 
    setContentView(tv);
 }
}

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