This chapter shows how to export keys from a KeyStore object using DidiSoft OpenPGP Library for .NET.
File name extensions
The commonly used file names extensions for OpenPGP keys are .asc for ASCII armored keys and .pgp for keys in binary format.
The export methods expect the export format of the key as a last parameter asciiArmour of type boolean with values (true) for ASCII armored format and (false) for the binary format.
List of examples
- Export a key pair (public and private key combined in one file)
- Export a public key
- Export a private key
- Export a stripped (partial) private key
All the export methods identify a key either with the key User Id (or part of it), the hexadecimal Key Id, or the raw key Id.
Export a key pair
A key pair consists of both the public key and its corresponding private key combined into a single file.
C# sample
using System; using DidiSoft.Pgp; public class ExportKeyPairDemo { public void Demo() { // initialize the KeyStore KeyStore ks = KeyStore.OpenFile(@"DataFiles\mykeys.store", "keystore password"); // should the exported files be ASCII or binary bool asciiArmored = true; // export both public and secret key with all sub keys in one file ks.ExportKeyRing(@"DataFiles\keypair.asc", "support@didisoft.com", asciiArmored); } } |
VB.NET sample
Imports System Imports DidiSoft.Pgp Public Class ExportKeyPairDemo Public Sub Demo() ' initialize the KeyStore Dim ks As KeyStore = KeyStore.OpenFile("DataFiles\keys.store", "keystore password") ' should the exported files be ASCII or binary Dim asciiArmored As Boolean = True ' export both public and secret key with all sub keys in one file ks.ExportKeyRing("DataFiles\keypair.asc", "support@didisoft.com", asciiArmored) End Sub End Class |
Export a public key
The example code below exports a public key by specifying part of the key User Id.
C# sample
using System; using DidiSoft.Pgp; public class ExportKeyPairDemo { public void Demo() { // initialize the KeyStore KeyStore ks = KeyStore.OpenFile(@"DataFiles\mykeys.store", "keystore password"); // should the exported files be ASCII or binary bool asciiArmored = true; // export public key having the specified userId // all its sub keys are exported too ks.ExportPublicKey(@"DataFiles\public_key_exported.asc", "support@didisoft.com", asciiArmored); } } |
VB.NET sample
Imports System Imports DidiSoft.Pgp Public Class ExportKeyPairDemo Public Sub Demo() ' initialize the KeyStore Dim ks As KeyStore = KeyStore.OpenFile("DataFiles\keys.store", "keystore password") ' should the exported files be ASCII or binary Dim asciiArmored As Boolean = True ' export public key having the specified userId ' all its sub keys are exported too ks.ExportPublicKey("DataFiles\public_key_exported.asc", "support@didisoft.com", asciiArmored) End Sub End Class |
Export a private key
The example code below exports a private key by specifying the hexadecimal Key Id.
C# sample
using System; using DidiSoft.Pgp; public class ExportPrivateKeyDemo { public void Demo() { // initialize the KeyStore KeyStore ks = KeyStore.OpenFile(@"DataFiles\key.store", "changeit"); // should the exported files be ASCII or binary bool asciiArmored = true; // export secret key, this is usually our own key. ks.ExportPrivateKey(@"DataFiles\private_key_exported.asc", "07907FF0360FE624C090765240B5F28BC8370827", asciiArmored); } } |
VB.NET sample
Imports System Imports DidiSoft.Pgp Public Class ExportKeys Public Sub Demo() ' initialize the KeyStore Dim ks As KeyStore = KeyStore.OpenFile("DataFiles\key.store", "changeit") ' should the exported files be ASCII or binary Dim asciiArmored As Boolean = True ' export secret key, this is usually our own key. ks.ExportPrivateKey("DataFiles\private_key_exported.asc", "07907FF0360FE624C090765240B5F28BC8370827", asciiArmored); End Sub End Class |
Export a partial private key
This sample code exports a partial private key, where the master private key material (its asymmetric algorithm parameters) is stripped. Such key can be used on another machine and if for some reason it gets compromised, the intruder cannot use it for signing other keys (this way compromising the Web of Trust).
A more detailed article related to stripped private keys can be found here Creating the perfect PGP key pair
C# sample
using System; using DidiSoft.Pgp; public class ExportPrivateKeyDemo { public void Demo() { // initialize the KeyStore KeyStore ks = KeyStore.OpenFile(@"DataFiles\key.store", "changeit"); // should the exported files be ASCII or binary bool asciiArmored = true; // export secret key, this is usually our own key. ks.ExportPartialPrivateKey(@"DataFiles\private_key_exported.asc", "asp@didisoft.com", asciiArmored); } } |
VB.NET sample
Imports System Imports DidiSoft.Pgp Public Class ExportKeys Public Sub Demo() ' initialize the KeyStore Dim ks As KeyStore = KeyStore.OpenFile("DataFiles\key.store", "changeit") ' should the exported files be ASCII or binary Dim asciiArmored As Boolean = True ' export secret key, this is usually our own key. ks.ExportPartialPrivateKey("DataFiles\private_key_exported.asc", "asp@didisoft.com", asciiArmored); End Sub End Class |
Summary
This chapter described how to export keys from a KeyStore.
You may also be interested in how to import OpenPGP keys in the DidiSoft KeyStore object.