Example usage for org.bouncycastle.openpgp.jcajce JcaPGPPublicKeyRingCollection getKeyRings

List of usage examples for org.bouncycastle.openpgp.jcajce JcaPGPPublicKeyRingCollection getKeyRings

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp.jcajce JcaPGPPublicKeyRingCollection getKeyRings.

Prototype

public Iterator<PGPPublicKeyRing> getKeyRings() 

Source Link

Document

return the public key rings making up this collection.

Usage

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 {/*w  ww  . ja  v a  2s .co  m*/
            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;
    }
}