Example usage for org.bouncycastle.openpgp PGPObjectFactory iterator

List of usage examples for org.bouncycastle.openpgp PGPObjectFactory iterator

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPObjectFactory iterator.

Prototype

public Iterator iterator() 

Source Link

Document

Support method for Iterable where available.

Usage

From source file:org.pgptool.gui.encryption.implpgp.EncryptionServicePgpImpl.java

License:Open Source License

@SuppressWarnings("rawtypes")
@Override/*from   w w w.j av a  2s . co  m*/
public Set<String> findKeyIdsForDecryption(InputStream inputStream) {
    Preconditions.checkArgument(inputStream != null, "Input stream must not be null");

    try {
        PGPObjectFactory factory = new PGPObjectFactory(PGPUtil.getDecoderStream(inputStream),
                KeyFilesOperationsPgpImpl.fingerprintCalculator);

        for (Iterator iter = factory.iterator(); iter.hasNext();) {
            Object section = iter.next();
            log.debug(section);

            if (section instanceof PGPEncryptedDataList) {
                PGPEncryptedDataList d = (PGPEncryptedDataList) section;
                HashSet<String> ret = new HashSet<>();
                for (Iterator dataIter = d.getEncryptedDataObjects(); dataIter.hasNext();) {
                    PGPPublicKeyEncryptedData data = (PGPPublicKeyEncryptedData) dataIter.next();
                    ret.add(KeyDataPgp.buildKeyIdStr(data.getKeyID()));
                }
                log.debug("Possible decryption with IDS: " + Arrays.toString(ret.toArray()));
                return ret;
            }
        }
        throw new RuntimeException("Information about decryption methods was not found");
    } catch (Throwable t) {
        throw new RuntimeException("This file doesn't look like encrypted file OR format is not supported", t);
    }
}

From source file:org.pgptool.gui.encryption.implpgp.EncryptionServicePgpImpl.java

License:Open Source License

@SuppressWarnings("rawtypes")
private PGPPublicKeyEncryptedData getPublicKeyEncryptedDataByKeyId(InputStream in, PGPSecretKey secretKey) {
    try {/*w  ww  . j a v a2s. c  om*/
        PGPObjectFactory factory = new PGPObjectFactory(PGPUtil.getDecoderStream(in),
                KeyFilesOperationsPgpImpl.fingerprintCalculator);

        for (Iterator iter = factory.iterator(); iter.hasNext();) {
            Object section = iter.next();
            if (section instanceof PGPEncryptedDataList) {
                PGPEncryptedDataList d = (PGPEncryptedDataList) section;
                for (Iterator dataIter = d.getEncryptedDataObjects(); dataIter.hasNext();) {
                    PGPPublicKeyEncryptedData data = (PGPPublicKeyEncryptedData) dataIter.next();
                    if (data.getKeyID() == secretKey.getKeyID()) {
                        return data;
                    }
                }
            }
        }
        // NOTE: That is actually should NEVER happen since secret key we're
        // supposed to use here was taken exactly same way as we're looking
        // for PGPPublicKeyEncryptedData now
        throw new RuntimeException("Encryption data matching given key "
                + KeyDataPgp.buildKeyIdStr(secretKey.getKeyID()) + " wasn't found");
    } catch (Throwable t) {
        throw new RuntimeException("Failed to find Encryption data section in encrypted file", t);
    }
}

From source file:org.pgptool.gui.encryption.implpgp.KeyFilesOperationsPgpImpl.java

License:Open Source License

@SuppressWarnings("rawtypes")
public static KeyDataPgp readKeyData(String filePathName) {
    KeyDataPgp data = new KeyDataPgp();

    try (FileInputStream stream = new FileInputStream(new File(filePathName))) {
        PGPObjectFactory factory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream),
                fingerprintCalculator);// www.  j av a2s  . c  om
        for (Iterator iter = factory.iterator(); iter.hasNext();) {
            Object section = iter.next();
            log.debug("Section found: " + section);

            if (section instanceof PGPSecretKeyRing) {
                data.setSecretKeyRing((PGPSecretKeyRing) section);
            } else if (section instanceof PGPPublicKeyRing) {
                data.setPublicKeyRing((PGPPublicKeyRing) section);
            } else {
                log.error("Unknown section enountered in a key file: " + section);
            }
        }
    } catch (Throwable t) {
        throw new RuntimeException("Error happenedd while parsing key file", t);
    }

    if (data.getPublicKeyRing() == null && data.getSecretKeyRing() == null) {
        throw new RuntimeException("Neither Secret nor Public keys were found in the input file");
    }

    return data;
}