Example usage for org.bouncycastle.openpgp PGPPublicKeyRing getPublicKeys

List of usage examples for org.bouncycastle.openpgp PGPPublicKeyRing getPublicKeys

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPPublicKeyRing getPublicKeys.

Prototype

public Iterator<PGPPublicKey> getPublicKeys() 

Source Link

Document

Return an iterator containing all the public keys.

Usage

From source file:SELSKeyGen.java

License:Open Source License

private static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException {
    in = PGPUtil.getDecoderStream(in);//from   www .  j a va 2s  . c o  m

    PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in);

    //
    // we just loop through the collection till we find a key suitable for encryption, in the real
    // world you would probably want to be a bit smarter about this.
    //
    PGPPublicKey key = null;

    //
    // iterate through the key rings.
    //
    Iterator rIt = pgpPub.getKeyRings();

    while (key == null && rIt.hasNext()) {
        PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();
        Iterator kIt = kRing.getPublicKeys();
        //boolean                        encryptionKeyFound = false;

        while (key == null && kIt.hasNext()) {
            PGPPublicKey k = (PGPPublicKey) kIt.next();

            if (k.isEncryptionKey()) {
                key = k;
            }
        }
    }

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

    return key;
}

From source file:alpha.offsync.security.OpenPGPSecurityUtility.java

License:Apache License

/**
 * Gets the correct encryption key from local public keyring using the
 * supplied key information.//from  www.j  a va  2  s. c om
 * 
 * @param keyInfo
 *            the supplied key information
 * @return the correct encryption key
 */
public PGPPublicKey getEncryptionKey(final String keyInfo) {
    PGPPublicKeyRingCollection pgpPub;
    try {
        pgpPub = new PGPPublicKeyRingCollection(
                PGPUtil.getDecoderStream(new FileInputStream(this.publicKeyRing)));

        final Iterator<PGPPublicKeyRing> keyRingIter = pgpPub.getKeyRings();
        while (keyRingIter.hasNext()) {
            final PGPPublicKeyRing keyRing = keyRingIter.next();
            final Iterator keyIter = keyRing.getPublicKeys();

            while (keyIter.hasNext()) {
                final PGPPublicKey key = (PGPPublicKey) keyIter.next();

                final Iterator idIter = key.getUserIDs();
                while (idIter.hasNext()) {
                    final String userID = idIter.next().toString();
                    if (userID.contains(keyInfo) && key.isEncryptionKey())
                        return key;
                }

            }
        }

    } catch (final FileNotFoundException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    } catch (final PGPException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:bisq.common.crypto.PGP.java

License:Open Source License

@Nullable
public static PGPPublicKey getPubKeyFromPem(@Nullable String pem) {
    if (pem != null) {
        InputStream inputStream = new ByteArrayInputStream(pem.getBytes(Charsets.UTF_8));
        try {/*from   w  w  w.  ja v a2 s  . c  om*/
            inputStream = PGPUtil.getDecoderStream(inputStream);
            try {
                JcaPGPPublicKeyRingCollection ringCollection = new JcaPGPPublicKeyRingCollection(inputStream);
                Iterator<PGPPublicKeyRing> keyRingsIterator = ringCollection.getKeyRings();
                while (keyRingsIterator.hasNext()) {
                    PGPPublicKeyRing pgpPublicKeyRing = keyRingsIterator.next();
                    Iterator<PGPPublicKey> pubKeysIterator = pgpPublicKeyRing.getPublicKeys();
                    while (pubKeysIterator.hasNext()) {
                        final PGPPublicKey pgpPublicKey = pubKeysIterator.next();
                        if ((pgpPublicKey).isEncryptionKey()) {
                            log.debug(pgpPublicKey.getClass().getName() + " KeyID: "
                                    + Long.toHexString(pgpPublicKey.getKeyID()) + " type: "
                                    + pgpPublicKey.getAlgorithm() + " fingerprint: "
                                    + new String(Hex.encode(pgpPublicKey.getFingerprint())));

                            BCPGKey bcKey = pgpPublicKey.getPublicKeyPacket().getKey();
                            log.debug(bcKey.getClass().getName());
                            if (bcKey instanceof RSAPublicBCPGKey) {
                                RSAPublicBCPGKey bcRSA = (RSAPublicBCPGKey) bcKey;
                                RSAPublicKeySpec specRSA = new RSAPublicKeySpec(bcRSA.getModulus(),
                                        bcRSA.getPublicExponent());
                                PublicKey jceKey = KeyFactory.getInstance("RSA").generatePublic(specRSA);
                                // if you want to use the key in JCE, use jceKey
                                // if you want to write "X.509" (SPKI) DER format to a file:
                                //Files.write(new File(pubKeyAsString).toPath(), jceKey.getEncoded());
                                // if you want to write in PEM, bouncycastle can do that
                                // or you can just do base64 and add BEGIN/END lines
                                // return pubKeyAsString; // assume only one key; if need to handle multiple keys
                                // or select other than the first, specify more clearly
                            }

                            return pgpPublicKey;
                        }
                    }
                }
                return null;
            } catch (PGPException | InvalidKeySpecException | NoSuchAlgorithmException e) {
                log.error("Error creating publicKey from pem. pem={}, error={}", pem, e);
                e.printStackTrace();
                throw new KeyConversionException(e);
            }

        } catch (IOException e) {
            log.error("Error creating publicKey from pem. pem={}, error={}", pem, e);
            e.printStackTrace();
            throw new KeyConversionException(e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException ignore) {
            }
        }
    } else {
        log.warn("Error creating publicKey from pem. pem=null");
        return null;
    }
}

From source file:cc.arduino.contributions.GPGDetachedSignatureVerifier.java

License:Open Source License

private PGPPublicKey readPublicKey(InputStream input, String keyId) throws IOException, PGPException {
    PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input),
            new BcKeyFingerprintCalculator());

    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 (Long.toHexString(key.getKeyID()).toUpperCase().endsWith(keyId)) {
                return key;
            }//from   w  ww .  j a v  a 2  s . c o m
        }
    }

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

From source file:com.arcusx.simplepgp.PgpKeyUtils.java

@SuppressWarnings("unchecked")
private static PGPPublicKey getFirstEncryptionKey(PGPPublicKeyRingCollection pgpPub) {
    Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings();
    while (rIt.hasNext()) {
        PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();
        Iterator<PGPPublicKey> kIt = kRing.getPublicKeys();
        while (kIt.hasNext()) {
            PGPPublicKey k = (PGPPublicKey) kIt.next();
            if (k.isEncryptionKey()) {
                return k;
            }// w  w  w .  j av  a2s. c o  m
        }
    }
    return null;
}

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

/**
 * Find the public key for the recipient
 *///from  ww w  .  j a  v  a 2  s .c o  m
public PGPPublicKey readPublicKey(PGPPublicKeyRingCollection pubRing, String recipient, boolean encrypting)
        throws IOException, PGPException {
    //
    // we just loop through the collection till we find a key suitable for encryption, in the real
    // world you would probably want to be a bit smarter about this.
    //
    PGPPublicKey key = null;

    //
    // iterate through the key rings.
    //
    @SuppressWarnings("unchecked")
    Iterator<PGPPublicKeyRing> rIt = pubRing.getKeyRings();

    //System.out.println("processing public key ring, looking for : "+recipient);
    while (key == null && rIt.hasNext()) {
        PGPPublicKeyRing kRing = rIt.next();
        //System.out.println("Found a ring with keys ");
        @SuppressWarnings("unchecked")
        Iterator<PGPPublicKey> kIt = kRing.getPublicKeys();

        //TODO bobby make sure it's safe to reuse the name from the prior key!
        String name = "<not specified>";
        while (key == null && kIt.hasNext()) {
            PGPPublicKey k = kIt.next();
            @SuppressWarnings("unchecked")
            Iterator<String> userIDs = k.getUserIDs();
            //                String name = "<not specified>";
            if (userIDs.hasNext()) {
                name = userIDs.next();
            }
            //System.out.println("found a key with name "+name);

            if (name.indexOf(recipient) >= 0) {
                if (!encrypting || k.isEncryptionKey()) {
                    //System.out.println("Found the key I'm looking for");
                    key = k;
                }
            }
        }
    }

    if (key == null) {
        if (encrypting) {
            throw new PGPException("Can't find encryption key in key ring");
        } else {
            throw new PGPException("Can't find signing key in key ring");
        }
    }

    return key;
}

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

public PGPPublicKey findFirstEncryptingKey(PGPPublicKeyRing keyRing) throws PGPException {
    @SuppressWarnings("unchecked")
    Iterator<PGPPublicKey> kIt = keyRing.getPublicKeys();

    PGPPublicKey retval = null;//from  ww  w .  java2 s  . c  o  m
    while (retval == null && kIt.hasNext()) {
        PGPPublicKey k = kIt.next();

        if (k.isEncryptionKey()) {
            //System.out.println("Found the key I'm looking for");
            retval = k;
        }
    }

    if (retval == null) {
        throw new PGPException("No encrypting key found in keyring");
    }

    return retval;
}

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

/**
 * Finds the first key in secretKeyRings which is capable of signing and which corresponds with a key in keyRing.
 * @param keyRing// w  ww.  j  a  v  a  2 s  .co  m
 * @param secretKeyRings
 * @return
 * @throws PGPException 
 */
public PGPSecretKey findFirstSigningKey(PGPPublicKeyRing keyRing, PGPSecretKeyRingCollection secretKeyRings)
        throws PGPException {
    @SuppressWarnings("unchecked")
    Iterator<PGPPublicKey> kIt = keyRing.getPublicKeys();

    PGPSecretKey retval = null;
    while (retval == null && kIt.hasNext()) {
        PGPPublicKey k = kIt.next();
        PGPSecretKey sk = secretKeyRings.getSecretKey(k.getKeyID());
        if (sk.isSigningKey()) {
            retval = sk;
        }
    }

    if (retval == null) {
        throw new PGPException("No signing key found");
    }

    return retval;
}

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

/**
 * Find the public keys for the recipient
 *//*from   w ww  . j  a v  a2s.c  o  m*/
public PGPPublicKeyRing findPublicKeyRing(PGPPublicKeyRingCollection pubRing, String recipient)
        throws IOException, PGPException {
    PGPPublicKeyRing retval = null;
    String retvalName = null;

    //
    // iterate through the key rings.
    //
    @SuppressWarnings("unchecked")
    Iterator<PGPPublicKeyRing> rIt = pubRing.getKeyRings();

    //System.out.println("processing public key ring, looking for : "+recipient);
    while (rIt.hasNext()) {
        PGPPublicKeyRing kRing = rIt.next();
        //System.out.println("Found a ring with keys ");
        @SuppressWarnings("unchecked")
        Iterator<PGPPublicKey> kIt = kRing.getPublicKeys();

        while (kIt.hasNext()) {
            PGPPublicKey k = kIt.next();

            String name = "<not specified>";

            @SuppressWarnings("unchecked")
            Iterator<String> userIDs = k.getUserIDs();
            if (userIDs.hasNext()) {
                name = userIDs.next();
            }
            //System.out.println("found a key with name "+name);

            if (name.indexOf(recipient) >= 0) {
                if (retval == null || retval == kRing) {
                    retval = kRing;
                    retvalName = name;
                } else {
                    throw new PGPException(
                            "Ambiguous recipient name; matches both " + name + " and " + retvalName);
                }
            }
        }
    }

    if (retval == null) {
        throw new PGPException("Can't find keyring matching " + recipient);
    }

    return retval;
}

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

public PGPPublicKey findFirstSigningKey(PGPPublicKeyRing keyRing) throws PGPException {
    @SuppressWarnings("unchecked")
    Iterator<PGPPublicKey> kIt = keyRing.getPublicKeys();

    PGPPublicKey retval = null;//from  w ww .j  av  a  2s  .  c  o m
    while (retval == null && kIt.hasNext()) {
        PGPPublicKey k = kIt.next();

        if (isSigningAlgorithm(k.getAlgorithm())) {
            retval = k;
        }
    }

    if (retval == null) {
        throw new PGPException("No signing key found in keyring");
    }

    return retval;
}