Generating pgp keys in Android

This article is an introduction how to generate an OpenPGP key pair with the help of DidiSoft OpenPGP Library for Android.

Example code

The generated key store is saved afterwards in file in the private context path.

The library can generate RSA and DH/DSS OpenPGP key pairs, and this is set by the third parameter of the generateKeyPair method.

import java.io.*;
import com.didisoft.pgp.*;
import com.didisoft.pgp.storage.*;
 
public class KeyStoreGenerateKeyDemo extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 
  TextView tvTmp = new TextView(this);
  tvTmp.append("Generating OpenPGP key ...");
  setContentView(tvTmp);
 
  TextView tv = new TextView(this);        
  // generate key
  KeyPairInformation key = null;
  try {                 
	key = this.generate(this);
  } catch (Exception e) {
	tv.append(e.getMessage());
  }
 
  if (key != null) {
	tv.append(key.getKeySize() + " bit " + key.getAlgorithm() + " key");
	tv.append("\n");
	tv.append("Key fingerprint: ");
	tv.append(key.getFingerprint());
	tv.append("\n");
	tv.append("Key ID hexadecimal: ");
	tv.append(key.getKeyIDHex());
	tv.append("\n");
	tv.append("Key User ID: ");
	tv.append(key.getUserIDs()[0]);
	tv.append("\n");            
  }
 
  tv.setMovementMethod(new ScrollingMovementMethod());        
  setContentView(tv);
 }
 
 /**
 * Generates an OpenPGP key pair into a KeyStore object 
 * saved into a private context file my.keystore
 */
 public KeyPairInformation generate(Context ctx) throws Exception, PGPException {
  // Key size in bits of the desired encryption key
  int keySizeInBits = 2048;
 
  // asymmetric encryption algorithm  
  KeyAlgorithm algorithm = KeyAlgorithm.RSA;
 
  // key primary user Id
  String userId = "demo2@didisoft.com";
 
  // preferred hashing algorithms
  HashAlgorithm[] hashingAlgorithms = new HashAlgorithm[]
					 {HashAlgorithm.SHA1,
			                  HashAlgorithm.SHA256,
					  HashAlgorithm.SHA384,
					  HashAlgorithm.SHA512};
 
  // preferred compression algorithms
  CompressionAlgorithm[] compressions = new CompressionAlgorithm[]
					{CompressionAlgorithm.ZIP,
					CompressionAlgorithm.ZLIB,
					CompressionAlgorithm.UNCOMPRESSED};
 
  // preferred symmetric key algorithms
  CypherAlgorithm[] cyphers = new CypherAlgorithm[] {
				  CypherAlgorithm.AES_128,
				  CypherAlgorithm.AES_192,
				  CypherAlgorithm.AES_256,
				  CypherAlgorithm.TWOFISH};
 
  // this is the password of the private key in the key pair
  String privateKeyPassword = "changeit";
 
  // create an in-memory key store
  KeyStore keyStore = new KeyStore(new AndroidContextFileKeyStorage(ctx, "my.keystore"));
 
	// invoke the key pair generation
	KeyPairInformation key = keyStore.generateKeyPair(keySizeInBits, 
					userId, 
					algorithm, 
					privateKeyPassword, 
					compressions, 
					hashingAlgorithms, 
					cyphers);
 
	return key;
 }
}

The complete example is available in the /Examples/src/android/KeyStoreGenerateKeyDemo.java file.

Summary

This article demonstrated how to generate an OpenPGP key pair with the help of DidiSoft OpenPGP Library for Android.

List of methods used:
KeyStore.generateKeyPair
KeyStore.saveToStream