An OpenPGP key consists of two parts – a public key that you give to your partners so they can send you encrypted data and a private key, which contains a stripped version of the public key (for backup purposes) and the private components needed to decrypt data encrypted with the public key.
The OpenPGP key format is a rich file format and allows in addition to the sole asymmetric cryptographic parameters the storage of many properties like User ID (a string describing the name and email of the key owner), key algorithm preferences, and many others including the possibility to store a JPEG image of the key owner.
+--------------------------+ +---------------------------+ | KeyPairInformation +-------+ com.didisoft.pgp.KeyStore | +------------^-------------+ +---------------------------+ | KeyStore.getKeys() +------------+-------------+ | PGPKeyPair | +--------------------------+
Above you can see the classes provided by DidiSoft OpenPGP Library for Java that are dedicated to .pgp keys:
com.didisoft.pgp.KeyPairInformation – represents a key and allows to observe its properties
com.didisoft.pgp.PGPKeyPair – inherits KeyPairInformation and is used to load and modify keys from the file system
com.didisoft.pgp.KeyStore – represents a key container and allows loading, modification and creation of keys
Having an OpenPGP key in a file on the disk (.asc file) we can perform some actions with it.
For example we can
- inspect it’s properties (Bit length, Key algorithm, associated User ID(s), creation time, etc.)
- check is there a private key or just a public key
- change the password of a private key
- export the whole key or only the public or private component
- see is the public key revoked or expired
Inspecting the properties of a key
This example shows how to inspect a few of the properties of an OpenPGP key located in a file on the disk.
A full list of the available properties can be found at the library JavaDoc page for KeyPairInformation and it’s subclass PGPKeyPair
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import com.didisoft.pgp.*; import com.didisoft.pgp.exceptions.*; public class PGPKeyDetails { public static void main(String[] args) { try { PGPKeyPair key = new PGPKeyPair("c:\\MyKeys\\pgp_key.asc"); System.out.println("Key algorithm"); System.out.println( key.getAlgorithm()); System.out.println("Bit length"); System.out.println( " " + key.getKeySize() + " bits"); System.out.println("Key ID hexadecimal"); System.out.println( key.getKeyIDHex()); System.out.println("Fingerprint"); System.out.println( key.getFingerprint()); System.out.println("Date created"); Calendar cal = Calendar.getInstance(); cal.setTime(key.getCreationTime()); StringBuffer date = new StringBuffer(); date.append(cal.get(Calendar.YEAR)).append('/') .append(cal.get(Calendar.MONTH)).append('/') .append(cal.get(Calendar.DATE)); System.out.println( date.toString()); System.out.println("Key User ID(s)"); for (int j=0; j < key.getUserIDs().length; j++) { System.out.println(key.getUserIDs()[j]); } if (key.isRevoked()) { System.out.println(" (Revoked)"); } if (key.isExpired()) { System.out.println(" (Expired)"); } } catch (NoPublicKeyFoundException e) { System.out.println("There is no OpenPGP key in the specified file"); } } } |