Getting from the gpg keyring is easy.
private PgpPublicKey GetPublicKey(String userId)
{
if (userId == null)
throw new PgpException("No recipient was specified");
PgpPublicKeyRingBundle publicKeyRingBundle = new
PgpPublicKeyRingBundle(new
MemoryStream(File.ReadAllBytes(PublicKeyRingPath)));
foreach (PgpPublicKeyRing keyRing in
publicKeyRingBundle.GetKeyRings(userId))
{
foreach (PgpPublicKey key in keyRing.GetPublicKeys())
{
if (key.IsEncryptionKey)
return key;
}
}
return null;
}
Similar for a private key (but you need the password):
private PgpPrivateKey GetPrivateKey(String userId, char[] passphrase)
{
if (userId == null)
throw new PgpException("No User ID was specified");
PgpSecretKeyRingBundle privateKeyRingBundle =
new
PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(new
MemoryStream(File.ReadAllBytes(PrivateKeyRingPath))));
PgpPrivateKey privateKey = null;
foreach (PgpSecretKeyRing keyRing in
privateKeyRingBundle.GetKeyRings(userId))
{
foreach (PgpSecretKey key in keyRing.GetSecretKeys())
{
privateKey = key.ExtractPrivateKey(passphrase);
if (privateKey != null)
return privateKey;
}
}
return null;
}
Have not yet worked out the 'adding' part, mostly for lack of time. At
present, I just dump the key to a file and farm out to the gpg command
line. The magic is probably somewhere in/around the KeyRingBundle classes,
however.
On Fri, Sep 19, 2014 at 9:59 AM, Paul Meyer <[email protected]> wrote:
> Hello,
>
> I'm relatively new to bouncy castle, so there may be something obvious
> I'm missing. For my project I need to be able to get and add PGP
> public keys to/from my RSA keyring file (generated by GnuPG) in string
> format using ID as the reference.
>
> For the first step (getting a key), I've posted the code that I tried here:
>
> http://stackoverflow.com/questions/25468450/bouncycastle-c-sharp-publickey-different-than-gnupg
>
> Can you help me with that?
>
> Thank you,
> Paul
>
>
--
_________________________________________________________
*Tony Bathgate | Developer 3, Digital Programming Group (formerly DSA) | RR
Donnelley*
1905 Horseshoe Rd, Lancaster, PA 17602
Office: *717.676.5501*
*[email protected] <[email protected]>*
http://www.rrdonnelley.com