2

I have a PGP key pair that I created with Kleopatra and I want to know how I can access it using C#?

I am using the PgpCore library to decrypt a file, but it appears I need to feed it the key file and I don't know where it is stored on the machine or the best way to access it.

Is there some way I can query Kleopatra with the ID of the certificate to retrieve the key?

ryansin
  • 1,735
  • 2
  • 26
  • 49
  • Can't you just export the key via the GUI and then load it in? – Brendan Green Aug 20 '19 at 07:59
  • @BrendanGreen Yes absolutely I could do it that way. Ideally I'd like to be able to programmatically access Kleopatra though if that's possible. – ryansin Aug 20 '19 at 08:14
  • @BrendanGreen When I try to export the key I can't seem to export it so that it remains encrypted with the pass phrase. Do you know if it is possible to export it from Kleopatra so that it remains encrypted? – ryansin Aug 20 '19 at 08:38

1 Answers1

0

Super late to this, but going through this myself. I think you'll have to export the keys, and then point at the file path where you store them to use in C#

Kleopatra

Export private key: right-click and select 'backup secret keys'

Export public key: right-click' export keys' to back up the public key.

PGPCore code for encryption and decryption:

Encryption using the public key:

// Load keys
EncryptionKeys encryptionKeys;
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
    encryptionKeys = new EncryptionKeys(publicKeyStream);

PGP pgp = new PGP(encryptionKeys);

// Reference input/output files
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\encrypted.pgp"))
    // Encrypt
    await pgp.EncryptStreamAsync(inputFileStream, outputFileStream);

Decryption using the private key and password:

// Load keys
EncryptionKeys encryptionKeys;
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
    encryptionKeys = new EncryptionKeys(privateKeyStream, "password");

PGP pgp = new PGP(encryptionKeys);

// Reference input/output files
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encryptedContent.pgp", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\decrypted.txt"))
    // Decrypt
    await pgp.DecryptStreamAsync(inputFileStream, outputFileStream);

Obviously change the file paths to the corresponding key path, , encrypt = public, decrypt = private

thatOneGuy
  • 9,977
  • 7
  • 48
  • 90