Skip to content

Instantly share code, notes, and snippets.

@jordanbaucke
Created July 26, 2014 14:26
Show Gist options
  • Save jordanbaucke/7ca1172c5238252c947e to your computer and use it in GitHub Desktop.
Save jordanbaucke/7ca1172c5238252c947e to your computer and use it in GitHub Desktop.
public static PGPPublicKey readPublicKey(InputStream input) throws IOException, PGPException {
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(
PGPUtil.getDecoderStream(input));
Iterator keyRingIter = pgpPub.getKeyRings();
while (keyRingIter.hasNext()) {
PGPPublicKeyRing keyRing = (PGPPublicKeyRing) keyRingIter.next();
Iterator keyIter = keyRing.getPublicKeys();
while (keyIter.hasNext()) {
PGPPublicKey key = (PGPPublicKey) keyIter.next();
if (key.isEncryptionKey()) {
return key;
}
}
}
throw new IllegalArgumentException(
"Can't find encryption key in key ring.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment