Parse Armored ECC public/private keys (generated from gpg cli) in java
If you just pass in the 'private key block', this will extract the ECPrivateKey:
private static ECPrivateKey privateKeyParse(byte[] privateKey) throws Exception
{
InputStream pgpIn = PGPUtil.getDecoderStream(new ByteArrayInputStream(privateKey));
PGPObjectFactory pgpFact = new PGPObjectFactory(pgpIn, new JcaKeyFingerprintCalculator());
PGPSecretKeyRing pgpSecRing = (PGPSecretKeyRing)pgpFact.nextObject();
PGPSecretKey pgpSec = pgpSecRing.getSecretKey();
PGPPrivateKey pgpPriv = pgpSec.extractPrivateKey(null);
return (ECPrivateKey)new JcaPGPKeyConverter().getPrivateKey(pgpPriv);
}
To answer a comment question as to how to how to get 'privateKey', if the entire:
-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----
is in a file, then just read the whole file into a byte[]:
InputStream fIn = new BufferedInputStream(new FileInputStream(...));
byte[] privateKey = org.bouncycastle.util.io.Streams.readAll(fIn);