Master keys

As of version 1.7.11.10 of DidiSoft OpenPGP Library for .NET, we can utilize a feature that offers automatic silent encryption with a list of additional keys called master keys. In this chapter you will find examples how to utilize this feature.

Table of contents
1. Registering a master key
2. Deleting a master key
3. Listing the currently registered master keys

1. Registering a master key

Master keys are registered per instance of the DidiSoft.Pgp.PGPLib class. Once registered they will be used transparently as additional encryption keys for each encryption and one pass signing and encryption.

C# example

using System;
using DidiSoft.Pgp;
 
public class RegisterMasterKey
{
  public static void Demo()
  {
	// create an instance of the library
	PGPLib pgp = new PGPLib();
 
	// Import the main company public key
	pgp.AddMasterKey(@"DataFiles\my_public_key.asc");
 
	// after the encryption the output file will be encrypted with two keys
	pgp.EncryptFile(@"INPUT.txt", @"partner_public_key.asc", @"OUTPUT.pgp");
  }
}

VB.NET example

Imports System
Imports DidiSoft.Pgp
 
Public Class RegisterMasterKey
 Public Shared Sub Demo()
  ' create an instance of the library
  Dim pgp As New PGPLib()
 
  ' Import the main company public key
  pgp.AddMasterKey("our_public_key.asc")
 
  ' after the encryption the output file will be encrypted with two keys
  pgp.EncryptFile("INPUT.txt", "recipient_public_key.asc", "OUTPUT.pgp")
 End Sub
End Class

Back to Top

2. Deleting a master key

Master keys are removed by specifying the index of the key to be removed. Each newly added key is registered as the next incremental index id : the first is index 0, the second is index 1 and so on.

The example below demonstrates how to remove the first master key:

C# example

using System;
using DidiSoft.Pgp;
 
public class UnregisterMasterKey
{
 public static void Demo()
 {
	PGPLib pgp = new PGPLib();
 
	// if there are any registered master keys, 
	// remove the first one
	if (pgp.GetMasterKeysCount() > 0)
	{
		pgp.DeleteMasterKey(0);
	}
 }
}

VB.NET example

Imports System
Imports DidiSoft.Pgp
 
Public Class UnregisterMasterKey
  Public Shared Sub Demo()
	Dim pgp As New PGPLib()
 
	' if there are any registered master keys, 
	' remove the first one
	If pgp.GetMasterKeysCount() > 0 Then
		pgp.DeleteMasterKey(0)
	End If
  End Sub
End Class

Back to Top

3. Listing the currently registered master keys

We can list the registered master keys and get each key as an instance of the KeyPairInformation object:

C# example

using System;
using DidiSoft.Pgp;
 
public class ListMasterKeys
{
 public static void Demo()
 {
  PGPLib pgp = new PGPLib();
 
  // list information about the registered master keys
  for (int i = 0; i < pgp.GetMasterKeysCount(); i++)
  {
	KeyPairInformation adkKey = pgp.GetMasterKey(i);
	Console.WriteLine(String.Format("Master key {0} User ID is : {1}; Fingerprint: {2}", 
									i, 
									adkKey.UserId, 
									adkKey.Fingerprint));
  }
 }
}

VB.NET example

Imports System
Imports DidiSoft.Pgp
 
Public Class ListMasterKeys
 Public Shared Sub Demo()
  Dim pgp As New PGPLib()
 
  ' list information about the registered master keys
  For i As Integer = 0 To pgp.GetMasterKeysCount() - 1
	Dim adkKey As KeyPairInformation = pgp.GetMasterKey(i)
	Console.WriteLine([String].Format("Master key {0} User ID is : {1}; Fingerprint: {2}", _
					i, _
					adkKey.UserId, _
					adkKey.Fingerprint))
  Next
 End Sub
End Class

Back to Top

Summary

In this chapter we have illustrated how to utilize master keys with OpenPGP Library for .NET.

This feature may be helpful for you, if for example you wish to have a list of additional recipients (like a company supervisor) that must also be able to decrypt the data intended for other parties, without explicitly specifying them on each encryption call.