Esempio n. 1
0
  private PGPPublicKey readPublicKey(InputStream publicKeyInputStream) throws SignatureException {
    try (InputStream publicKeyDecoderStream = getDecoderStream(publicKeyInputStream)) {
      final PGPPublicKeyRingCollection pgpPub =
          new PGPPublicKeyRingCollection(publicKeyDecoderStream);
      PGPPublicKey key = null;

      Iterator rIt = pgpPub.getKeyRings();

      while (key == null && rIt.hasNext()) {
        PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();
        Iterator kIt = kRing.getPublicKeys();
        while (key == null && kIt.hasNext()) {
          key = (PGPPublicKey) kIt.next();
        }
      }

      if (key == null) {
        throw new IllegalArgumentException("Can't find encryption key in key ring.");
      }

      return key;
    } catch (IOException | PGPException e) {
      throw new SignatureException("Failed to read public key", e);
    }
  }
 private static PGPPublicKey selectKey(final PGPPublicKeyRingCollection rings) {
   for (final Iterator<?> ri = rings.getKeyRings(); ri.hasNext(); ) {
     final PGPPublicKeyRing currRing = (PGPPublicKeyRing) ri.next();
     for (final Iterator<?> ki = currRing.getPublicKeys(); ki.hasNext(); ) {
       final PGPPublicKey k = (PGPPublicKey) ki.next();
       if (k.isEncryptionKey()) {
         return k;
       }
     }
   }
   return null;
 }
Esempio n. 3
0
 public PublicKeyRingImpl readPublicKeyRingFromAsciiArmor(final String keytext) {
   PGPPublicKeyRingCollection publicKeyRingCollection = null;
   try {
     publicKeyRingCollection = readPublicKey(keytext);
   } catch (Exception e) {
     new RuntimeException(e);
   }
   Iterator keyRings = publicKeyRingCollection.getKeyRings();
   if (keyRings.hasNext()) {
     PGPPublicKeyRing pgpPublicKeyRing = (PGPPublicKeyRing) keyRings.next();
     PublicKeyRingImpl publicKeyRing = convert(pgpPublicKeyRing);
     publicKeyRing.setRawData(keytext);
     return publicKeyRing;
   }
   return null;
 }
  static PGPPublicKey readPublicKey(String keyringPath) throws Exception {
    InputStream input = new ByteArrayInputStream(getKeyRing(keyringPath));
    PGPPublicKeyRingCollection pgpPub =
        new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input));

    @SuppressWarnings("rawtypes")
    Iterator keyRingIter = pgpPub.getKeyRings();
    while (keyRingIter.hasNext()) {
      PGPPublicKeyRing keyRing = (PGPPublicKeyRing) keyRingIter.next();

      @SuppressWarnings("rawtypes")
      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.");
  }