Search code examples
c#cryptographygnupgopenpgp

Importing a private RSA key generated with GnuPG into C#


I want to sign some things within my C# application, but I need to do so with a private RSA key that I have previously generated with GnuPG instead of one new key generated within the application itself.

I can export the RSA key with GnuPG and get something like (please, note I have omitted most lines here, is just an example):

-----BEGIN PGP PRIVATE KEY BLOCK-----
lQOYBFXlhWwBCACfOGAw5Qr5ddFvDFZlDmys18KRV3XawArMiPe4hzivsEB3h+M1
df12Pz3l6IWnUJ/nJt/ZohwCOjm93+zT3xmGcAL9mh/lez6+UoQB8uB0hJ1ltLnZ
8RumvpExXJ2c6LfmaLrwyLHLUSAu8mfV6KoLtD9OxHkIdHktKpBzIPkLG9lRNAmN
kzjI9sz7pLq80+YevPA60niI0SBwbmJTHluvEQB32BkcEQ==
=u3H/
-----END PGP PRIVATE KEY BLOCK-----

Now the question is: how I could import that into C# cryptography subsystem so I can use it for signing things?


Solution

  • The built-in crypto-library of C# only supports X.509, not OpenPGP; both are incompatible although principally using the same cryptographic algorithms. You might be able to extract the RSA primes and import them somehow, but the libraries will still not be able to produce valid OpenPGP output.

    Use the Bouncy Castle library instead, which is an OpenPGP implementation for C# (or interface GnuPG, for example through GPGME, but the C# binding is still an alpha version).