---
title: "Create an OpenPGP key revocation certificate in C#"
url: https://recursiveai.org/onecrypto/cookbook/create-openpgp-key-revocation-certificate/
description: "Use Pgp.Revoke in C# to sign a primary-key revocation and export the revoked OpenPGP public certificate without exporting secret key material."
version: "0.4.0-preview.2"
---

# Create an OpenPGP key revocation certificate 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/create-openpgp-key-revocation-certificate/create-openpgp-key-revocation-certificate.zip)

`Pgp.Revoke` signs a notice to stop using the key. Share it as a public certificate, not a private key.

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

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

```csharp
using RecursiveAI.OneCrypto;

var key = Pgp.GenerateKey("Demo key");
var revoked = Pgp.Revoke(key);
File.WriteAllBytes("revoked.asc",
    revoked.ExportPublic(armor: true));
Console.WriteLine("Wrote revoked.asc");
```

[The example](https://recursiveai.org/onecrypto/cookbook/create-openpgp-key-revocation-certificate/create-openpgp-key-revocation-certificate.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
Wrote revoked.asc
```

## The full program

The download generates a throwaway key in memory, writes `revoked.asc`, and reads that public file back to show its primary-key state. It doesn’t read or save a private key file.

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

var key = Pgp.GenerateKey("Demo key");
var revoked = Pgp.Revoke(key);
File.WriteAllBytes("revoked.asc",
    revoked.ExportPublic(armor: true));
Console.WriteLine("Wrote revoked.asc");

var saved = PgpCertificate.Parse(
    File.ReadAllBytes("revoked.asc"));
Console.WriteLine(saved.Primary.StateAt(DateTimeOffset.UtcNow).Reason);
Console.WriteLine($"Secret material: {saved.HasSecretKeyMaterial}");
```

`dotnet run` prints:

```text
Wrote revoked.asc
The key is revoked.
Secret material: False
```

## Variations

**Revoke a key you already own.** Export that key to `secret.asc`. For example, replace `YOUR_KEY_FINGERPRINT` with the fingerprint of the key you mean to revoke:

```shell
(umask 077; gpg --armor --export-secret-keys YOUR_KEY_FINGERPRINT > secret.asc)
```

Replace `Program.cs` with the code below and run it in the folder containing `secret.asc`. If the exported key is protected, supply its passphrase in the `PGP_KEY_PASSWORD` environment variable; leave that variable unset for an unprotected key. Keep `secret.asc` private. Share only `revoked.asc`, and only when you intend others to stop using the key.

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

var key = PgpCertificate.Parse(
    File.ReadAllBytes("secret.asc"));
var password = Environment.GetEnvironmentVariable(
    "PGP_KEY_PASSWORD");
var revoked = Pgp.Revoke(key, password);
File.WriteAllBytes("revoked.asc",
    revoked.ExportPublic(armor: true));
Console.WriteLine("Wrote revoked.asc");
```

With a throwaway exported key, `dotnet run` printed:

```text
Wrote revoked.asc
```

**Use a password-protected key.** Replace the key generation and revocation lines with these. The password is for this throwaway key only.

```csharp
const string password = "demo password";
var key = Pgp.GenerateKey("Demo key", password);
var revoked = Pgp.Revoke(key, password);
```

If the password doesn’t unlock the secret primary key, `Pgp.Revoke` throws `PgpException` before the example writes the file.

## Related recipes

- [Generate an OpenPGP key](https://recursiveai.org/onecrypto/cookbook/generate-pgp-key/)
- [Sign and verify an OpenPGP message](https://recursiveai.org/onecrypto/cookbook/pgp-sign-verify/)
- [Read a GnuPG keyring](https://recursiveai.org/onecrypto/cookbook/read-gnupg-keyring/)

## The short answer

Install `RecursiveAI.OneCrypto.OpenPgp`, call `Pgp.Revoke` with the certificate that holds your secret primary key, and export the result with `ExportPublic(armor: true)`. The output is a text-armored public key block carrying a signed primary-key revocation. It is not a bare revocation signature. Creating the file does not publish it or change copies that other people already hold.

## What does revoking an OpenPGP key mean?

A revocation is a signed statement about a key, not a deletion. The public key still identifies the same key, but people and software that receive and accept the revocation know to stop using it. `Pgp.Revoke` creates a hard revocation with “no reason given.” In OneCrypto’s [key-management rules](https://recursiveai.org/onecrypto/docs/openpgp/#key-management), the key counts as revoked even when OneCrypto checks a signature made before the revocation, so it isn’t a way to retire a key only from today onward. It applies to the primary key, not just one user ID or subkey. To set a reason or make a soft revocation (for example, `PgpRevocationReason.Retired`), use `PgpKeyEditor.RevokeKey` instead.

The file can be sent to the people who use the key or distributed through the channels where you published the original. If you are preparing a revocation as a backup instead, keep it private until you intend to revoke the key. It contains no secret key material, but someone who obtains the file can distribute the signed revocation.

## Can you revoke an OpenPGP key without the private key?

You need the secret primary key to make a **new** revocation with `Pgp.Revoke`. An existing valid revocation can still be shared if you later lose access to that key. A public certificate without a revocation is not enough to create one, even if you know the fingerprint or still have a secret subkey.

If your workflow calls for a smaller revocation certificate, OneCrypto distinguishes the full public certificate returned by `Pgp.Revoke` from the compact form from `PgpKeyEditor.CreateRevocationCertificate`: the public primary key plus one key revocation signature. Both are distinct from a bare signature packet. See the [OpenPGP docs](https://recursiveai.org/onecrypto/docs/openpgp/) and [RFC 9580’s key revocation signature type](https://www.rfc-editor.org/rfc/rfc9580.html#section-5.2.1.11).
