Created
May 8, 2013 22:26
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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