Revoke a pgp key in Java

In this example we are going to show how to revoke a public OpenPGP key directly. For this purpose we have to posses the corresponding private key and know its password. In cases when we have lost the password or the private key a revocation certificate or a designated revoker key must be used.

Revoke pgp public key located in a file.

This example demonstrates revoking a key located in file on the disk:

import com.didisoft.pgp.*;
 
public class RevokeKeyDemo {
 public static void main(String args[]) throws Exception {
  // create an instance of the library
  RevocationLib lib = new RevocationLib();
 
  // public key to be revoked
  String targetPublicKey = "key1_public.asc";
 
  // private key corresponding to the public key we want
  // to revoke
  String targetPrivateKey = "key1_private.asc";
  String targetPrivateKeyPassword = "password"; 
 
  // revocation reason and description
  byte revocationCode = RevocationLib.REASON_KEY_NO_LONGER_USED;
  String revocationDescription = "This key is no longer used";
 
  // revoke key directly
  lib.revokeKey(targetPublicKey,
		targetPrivateKey,
		targetPrivateKeyPassword,
		revocationCode,
		revocationDescription);
 }
}

The method revokeKey above requires that the public key file is writable in order to append the revocation signature.

Revoke pgp public key located in a KeyStore file.

import com.didisoft.pgp.KeyStore;
import com.didisoft.pgp.RevocationLib;
 
public class KeyStoreRevokeKey {
 public static void main(String[] args) throws Exception {
  // create an instance of the library
  RevocationLib lib = new RevocationLib();
 
  // initialize the KeyStore
  KeyStore keyStore = new KeyStore("demo.keystore", "password");
 
  // a public key and corresponding private key
  // with this User Id should exist in this KeyStore
  String targetKeyUserId = "RSA_1";
  String targetPrivateKeyPassword = "password";
 
  // revocation reason and description
  byte revocationCode = RevocationLib.REASON_KEY_NO_LONGER_USED;
  String revocationDescription = "This key is no longer used";
 
  // revoke key directly
  lib.revokeKey(keyStore,
		targetKeyUserId,
		targetPrivateKeyPassword,
		revocationCode,
		revocationDescription);
 }
}

Afterwords the public key ca be exported and distributed in order our partners to avoid its usage.

(This functionality is available from version 2.5.4)