---
title: "Read a GnuPG public keybox (.kbx) in C#"
url: https://recursiveai.org/onecrypto/cookbook/read-gnupg-keybox/
description: "Read a GnuPG public keybox (.kbx) in C# and list OpenPGP fingerprints and user IDs directly from pubring.kbx without running GnuPG."
version: "0.4.0-preview.2"
---

# Read a GnuPG public keybox (.kbx) in C#

**OneCrypto** is a free, open-source cryptography library for .NET from Recursive AI. MIT or Apache-2.0, preview, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCrypto.OpenPgp) · [GitHub](https://github.com/recursive-ai-oss/onecrypto-csharp) · [Docs](https://recursiveai.org/onecrypto/docs/) · [Download example (.zip)](https://recursiveai.org/onecrypto/cookbook/read-gnupg-keybox/read-gnupg-keybox.zip)

```shell
dotnet add package RecursiveAI.OneCrypto.OpenPgp --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneCrypto.OpenPgp.GnuPG;
var box = Keybox.Parse(
    File.ReadAllBytes("pubring.kbx"));
foreach (var blob in box.OpenPgpBlobs)
    Console.WriteLine(Convert.ToHexString(
        blob.PrimaryFingerprint.Span));
```

[The example](https://recursiveai.org/onecrypto/cookbook/read-gnupg-keybox/read-gnupg-keybox.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

```text
8E9E6980A754AFA6716D5F41FB1577AFDEAA66F1
```

If you paste the code, copy the sample `pubring.kbx` from the download into the project folder too.

## The full program

The sample was generated with GnuPG for the fictional user ID `Keybox Example <keybox@example.org>`. It starts with `00 00 00 20 01 01 00 02 4B 42 58 66` (`4B 42 58 66` spells `KBXf`). The download lists the fingerprint and user IDs. To inspect another file, run `dotnet run -- path/to/pubring.kbx`.

```csharp
using RecursiveAI.OneCrypto.OpenPgp;
using RecursiveAI.OneCrypto.OpenPgp.GnuPG;

string path = args.Length > 0 ? args[0] : "pubring.kbx";
var box = Keybox.Parse(File.ReadAllBytes(path));
foreach (var blob in box.OpenPgpBlobs)
{
    Console.WriteLine(Convert.ToHexString(blob.PrimaryFingerprint.Span));
    var certificate = PgpCertificate.FromTransferable(blob.ReadKey());
    foreach (string userId in certificate.UserIds)
        Console.WriteLine($"  {userId}");
}
```

`dotnet run` prints:

```text
8E9E6980A754AFA6716D5F41FB1577AFDEAA66F1
  Keybox Example <keybox@example.org>
```

`Keybox.Parse` throws `GnuPGFormatException` if the container is malformed; `ReadKey()` can throw `OpenPgpParseException` if the OpenPGP data inside it is malformed.

## Variations

**Count the X.509 records.** The API exposes them separately from OpenPGP records.

```csharp
using RecursiveAI.OneCrypto.OpenPgp.GnuPG;

var box = Keybox.Parse(File.ReadAllBytes("pubring.kbx"));
Console.WriteLine(box.X509Blobs.Count());
```

The sample prints `0`.

## Related recipes

- [Read public keys from a GnuPG home](https://recursiveai.org/onecrypto/cookbook/read-gnupg-keyring/)
- [Verify an OpenPGP signature](https://recursiveai.org/onecrypto/cookbook/pgp-sign-verify/)

## The short answer

Read the `.kbx` file into a byte array, pass it to `Keybox.Parse`, and enumerate `OpenPgpBlobs`. Each record exposes `PrimaryFingerprint`; use `ReadKey()` to get the OpenPGP packets when you also need user IDs. If you have the file already, there is no need to import it into another keyring or launch GnuPG.

## What is `pubring.kbx`?

It’s a GnuPG public-key container, not a single exported key. A keybox contains a header and a sequence of binary records, called blobs. The [OneCrypto format notes](https://recursiveai.org/onecrypto/docs/gnupg-home/) describe the supported OpenPGP and X.509 records. OpenPGP records can be decoded as keys. X.509 records expose their certificate bytes as DER; they do not appear in `OpenPgpBlobs`.

The file path matters. Pass the keybox file itself, not the GnuPG home directory. A legacy `pubring.gpg` or binary `gpg --export` stream is a sequence of transferable keys instead of a keybox; use `LegacyKeyring` for that format. If your input is a home directory and you want the library to choose between `pubring.kbx` and `pubring.gpg`, use `GnuPGHome.ReadPublicKeys()` instead.

## How do I display GnuPG public-key details without importing them?

Read the public file and print only the fields you need. The short snippet uses the fingerprint in each record’s metadata. The full example decodes each OpenPGP key and reads the user IDs from its certificate, so it can show more than one per key. No private-key file, passphrase or running GnuPG process is involved.

Treat the result as an inventory, not a trust decision. A user ID is text associated with the key, not proof of ownership of the name or email address. Reading the public keybox also doesn’t read the separate `trustdb.gpg` ownertrust records. If you expected OpenPGP keys but the list is empty, check whether the file contains X.509 records or whether you opened a different keybox from the one you meant to inspect.
