简体   繁体   中英

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?

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).

What you quoted is not exactly an "RSA key" but an armored OpenPGP private key which probably includes RSA key material and also includes other stuff.

As said in the other answer, you will have hard time importing it into C# (especially in Unity) without a third-party library. If you need this as a one-time operation, you can take our SecureBlackbox (evaluation will be enough), load the OpenPGP key, then take the key material and save it to format which you can load to Unity later.

If you plan to perform such operations on a regular basis, it makes sense to write key generator in C# and generate RSA keys suitable for your needs without OpenPGP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM