pgp key photo image in C# and VB.NET

The OpenPGP standard (rfc 4880) defines a way to embed images within OpenPGP public keys. This was designed so that the OpenPGP key file can also be used to remind us visually who’s its owner.

Unfortunately, the only supported by the OpenPGP standard file format for images is JPEG.

In this chapter we are going to demonstrate how to work with image files in association with OpenPGP keys.

(This functionality is available as of version 1.7.8 of DidiSoft OpenPGP Library for .NET)

Table of contents

1. Add an image file to an OpenPGP key.
2. Display the images contained in an OpenPGP key
3. Delete an image from an OpenPGP key

1. Add an image file to an OpenPGP key.

In order to add an image file to an OpenPGP key, we have to import the key into a KeyStore object. It must be noted that we must also have the private key component of the public key that we wish to associate the image with.

The JPEG image file data has to be read into an array of bytes and then added to a key:

C# example

using System;
using System.IO;
using DidiSoft.Pgp;
 
public class KeyStoreImages
{
 public static void Demo()
 {
	// here we create an in-memory located KeyStore
	KeyStore ks = KeyStore.OpenInMemory();
	ks.ImportPublicKey(@"DataFiles\public_key.asc");
	ks.ImportPrivateKey(@"DataFiles\private_key.asc");
 
	// add a JPEG image to an imported key
	KeyPairInformation[] keys = ks.GetKeys();
	KeyPairInformation key = keys[0];
	string imageLocation = @"c:\Pictures\Me.jpg";
	byte[] jpegImageBytes = File.ReadAllBytes(imageLocation);
	ks.AddJpegImage(key.KeyId, "changeit", jpegImageBytes);
 }
}

VB.NET example

Imports System
Imports System.IO
Imports DidiSoft.Pgp
 
Public Class KeyStoreImages
 Public Shared Sub Demo()
	' here we create an in-memory located KeyStore
	Dim ks As KeyStore = KeyStore.OpenInMemory()
	ks.ImportPublicKey("DataFiles\public_key.asc")
	ks.ImportPrivateKey("DataFiles\private_key.asc")
 
	' add a JPEG image to an imported key
	Dim keys As KeyPairInformation() = ks.GetKeys()
	Dim key As KeyPairInformation = keys(0)
	Dim imageLocation As String = "c:\Pictures\Me.jpg"
	Dim jpegImageBytes As Byte() = File.ReadAllBytes(imageLocation)
	ks.AddJpegImage(key.KeyId, "changeit", jpegImageBytes)
 End Sub
End Class

2. Display the images contained in an OpenPGP key

In the example below we load the first image contained in an OpenPGP key (if exists) into a System.Drawing.Image object.

C# example

 // Assuming keyStore is an instance of a DidiSoft.Pgp.KeyStore
 if (keyStore.GetJpegImagesCount(publicKeyId) [/arrow] 0)
 {
    int imageId = 0; // the first photo
    pictureBox1.Image = Image.FromStream(new MemoryStream(keyStore.GetJpegImageData(publicKeyId, imageId)));
 }

VB.NET example

 ' Assuming keyStore is an instance of a DidiSoft.Pgp.KeyStore
 If (keyStore.GetJpegImagesCount(publicKeyId) [chr num="64"] 0) Then
    Dim imageId As Integer = 0 ' the first photo
    pictureBox1.Image = Image.FromStream(new MemoryStream(keyStore.GetJpegImageData(publicKeyId, imageId)))
 End If

3. Delete image from an OpenPGP key

In order to delete an image from an OpenPGP key we have to specify its index sequence. If we wish to delete all images contained into an OpenPGP key we shall start from the last one as you can see from this example :

C# example

//  Assuming keyStore is an instance of a DidiSoft.Pgp.KeyStore
 for (int i=(keyStore.GetJpegImagesCount(publicKeyId)-1); i > -1; i--)
 {
    keyStore.DeleteJpegImage(publicKeyId, imageId);
 }

VB.NET example

 ' Assuming keyStore is an instance of a DidiSoft.Pgp.KeyStore
 For i As Integer = (keyStore.GetJpegImagesCount(publicKeyId) - 1) To 0 Step -1
   keyStore.DeleteJpegImage(publicKeyId, imageId)
 Next

Summary

This chapter demonstrated how to associate JPEG photo images with OpenPGP keys using DidiSoft OpenPGP Library for .NET

You may also check our example applications that ship with the library in the <library installation path>/Examples sub folder:

KeyToolsCS(VB) – demonstrates how to add a JPEG photo in an OpenPGP key and how to see it.
PGPLibExampleCS(VB)/KeyStoreImages.cs(.vb) –   shows how to add a JPEG photo in an OpenPGP key

List of methods used:

KeyStore.AddJpegImage Adds a JPEG image file into an OpenPGP key
KeyStore.GetJpegImagesCount Gets the number of JPEG images contained in an OpenPGP key
KeyStore.GetJpegImageDate Gets the serialized JPEG image data from an OpenPGP key
KeyStore.DeleteJpegImage Removes a JPEG image from an OpenPGP key