Skip to content

Instantly share code, notes, and snippets.

@kelvinewilliams
Created May 8, 2013 22:26
Show Gist options
  • Save kelvinewilliams/5544157 to your computer and use it in GitHub Desktop.
Save kelvinewilliams/5544157 to your computer and use it in GitHub Desktop.
Retrieve and return a PGPPrivateKey from an ASCII-Armored block (in this case on disk).
private PGPPrivateKey getPrivateKey(Long keyId, String keyPassphrase) {
PGPSecretKeyRing pgpSecretKeyRing = null;
PGPPrivateKey pgpPrivateKey = null;
PGPSecretKey pgpSecretKey = null;
try {
FileInputStream fis = new FileInputStream("/tmp/sec9.asc");
ArmoredInputStream ais = new ArmoredInputStream(fis);
pgpSecretKeyRing = new PGPSecretKeyRing(ais, new BcKeyFingerprintCalculator());
pgpSecretKey = pgpSecretKeyRing.getSecretKey(keyId);
PBESecretKeyDecryptor pbeSecretKeyDecryptor =
new BcPBESecretKeyDecryptorBuilder(
new BcPGPDigestCalculatorProvider()).build(keyPassphrase.toCharArray());
pgpPrivateKey = pgpSecretKey.extractPrivateKey(pbeSecretKeyDecryptor);
System.out.println("Private Key ID: " + pgpPrivateKey.getKeyID());
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace();
} catch (PGPException e) {
e.printStackTrace();
}
return pgpPrivateKey;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment