Inspecting a pgp key in C# and VB.NET

In this chapter, we will examine how to observe the properties of an OpenPGP key.

Table of contents
1. Introduction to OpenPGP keys
2. Properties of a key
3. Example code

1. Introduction to OpenPGP keys

An OpenPGP key consists of two parts called а public key and private (secret) key. Together they are called a key pair.

The public key is used for encrypting and signature verification.

The private key is used for decrypting and digital signing. It is protected with a password as an additional level of security.

File names

The most common form of OpenPGP keys distribution is in ASCII armored files with filename extension .asc. Such files may contain a public key, a private key or both keys.

Structure 

In the days of PGP(r) 2.6 an OpenPGP public or private key contained only one component. In contrast, keys created with more recent OpenPGP implementations contain additional subkeys that have different Key ID and optionally may have additional User ID associated with them. Each public subkey has its corresponding private subkey, as you can see from the diagram below:

Illustrates the structure of an OpenPGP key

An interesting thing that can be seen from the above diagram is that a private key contains its corresponding public key within itself, but without the key preferences (e.g. preferred compression algorithm, hashing algorithm, etc.). This way if for some reason we have lost our public key we can always restore it from the private key, by exporting it.

2. Properties of a key

DidiSoft OpenPGP Library for .NET offers a class DidiSoft.Pgp.KeyPairInformation that wraps OpenPGP keys and provides property members for inspecting a key.

Here are some of its most common members:

  • AlgorithmType – the key asymmetric encryption algorithm
  • CreationTime – when was the key created
  • ExpirationTime – the key expiration date
  • Fingerprint – a hexadecimal string forming the unique key fingerprint
  • IsExpired – if the key has an expiration time, has it already occurred

 

3. Example code

Having a key in a file, we can programmatically inspect its properties. The examples below illustrate how to do that with a key located in a file. We are going to use a subclass of DidiSoft.Pgp.KeyPairInfomration called PGPKeyPair.

C# example

using System;
using DidiSoft.Pgp;
 
public class KeyPropertiesDemo
{
 public static void Main(string[] args)
 {
  PGPKeyPair key = new PGPKeyPair(@"c:\key.asc");
 
  Console.WriteLine("Key Id is {0}", key.KeyIdHex);            
  Console.WriteLine("Key primary User Id is {0}", key.UserId);
  if (key.UserIds.Length > 1)
  {
    Console.WriteLine("Additional User Id's associated with this key");
    for (int i = 1; i < key.UserIds.Length; i++)
    {
	Console.WriteLine(key.UserIds[i]);
    }
  }
  Console.WriteLine("Key size in bits is {0}", key.KeySize);
  Console.WriteLine("Key algorithm is {0}", key.Algorithm);
  Console.WriteLine("Key was created on {0}", key.CreationTime);
 
  if (key.HasPrivateKey) {
	Console.WriteLine("Contains both public and private key");
  } else {
	Console.WriteLine("Contains a public key only");
  }
 
  if (key.IsExpired) {
	Console.WriteLine("Key is expired");
  }
  if (key.Revoked)
  {
	Console.WriteLine("Key is revoked");
  }
 
  Console.WriteLine("Key contains {0} sub keys", key.PublicSubKeys.Length);
 }
}

VB.NET code

Imports System
Imports DidiSoft.Pgp
 
Public Class Class1
 Public Shared Sub Main(ByVal args As String())
  Dim key As New PGPKeyPair("c:\key.asc")
 
  Console.WriteLine("Key Id is {0}", key.KeyIdHex)
  Console.WriteLine("Key primary User Id is {0}", key.UserId)
  If key.UserIds.Length > 1 Then
    Console.WriteLine("Additional User Id's associated with this key")
    For i As Integer = 1 To key.UserIds.Length - 1
	Console.WriteLine(key.UserIds(i))
    Next
  End If
  Console.WriteLine("Key size in bits is {0}", key.KeySize)
  Console.WriteLine("Key algorithm is {0}", key.Algorithm)
  Console.WriteLine("Key was created on {0}", key.CreationTime)
 
  If key.HasPrivateKey Then
	Console.WriteLine("Contains both public and private key")
  Else
	Console.WriteLine("Contains a public key only")
  End If
 
  If key.IsExpired Then
	Console.WriteLine("Key is expired")
  End If
  If key.Revoked Then
	Console.WriteLine("Key is revoked")
  End If
 
  Console.WriteLine("Key contains {0} sub keys", _
                    key.PublicSubKeys.Length)
 End Sub
End Class

Back to Top

Summary

In this chapter, we have discussed how to inspect the properties of an OpenPGP key.

The PGPKeyPair class also provides methods for key generation, exporting, and private key password change.

You may also check the sample KeyToolCS(VB) WinForms application that ships with the library, for demonstration of operating with OpenPGP keys.