Example usage for org.bouncycastle.openpgp PGPPublicKeyRing PGPPublicKeyRing

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

Introduction

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

Prototype

public PGPPublicKeyRing(List pubKeys) 

Source Link

Document

Base constructor from a list of keys representing a public key ring (a master key and its associated sub-keys).

Usage

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

License:Open Source License

/**
 * Get the keyring pointed to by the public & secret files.  Creates the files
 * with a single key if they don't already exist; interrogates keyDataSource for the
 * info to create the key (typically it should pop up a gui).
 * /*from   www .ja v  a2 s  .  c om*/
 * TODO bobby: doesn't create usable keyrings yet, and doesn't save what it does create :-( 
 * @param publicKeyRingFile
 * @param secretKeyRingFile
 * @param keyDataSource
 * @return
 * @throws PGPException
 * @throws FileNotFoundException
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 * @throws InvalidAlgorithmParameterException
 */
public Pair<PGPPublicKeyRingCollection, PGPSecretKeyRingCollection> getOrCreateKeyring(File publicKeyRingFile,
        File secretKeyRingFile, KeyDataSource keyDataSource) throws PGPException, FileNotFoundException,
        IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
    boolean pubRingFound = publicKeyRingFile.isFile();
    boolean secRingFound = secretKeyRingFile.isFile();

    if (pubRingFound != secRingFound) {
        throw new PGPException("Expect both public & secret keyring, or neither: " + publicKeyRingFile + ", "
                + secretKeyRingFile);
    }

    Pair<PGPPublicKeyRingCollection, PGPSecretKeyRingCollection> retval = new Pair<PGPPublicKeyRingCollection, PGPSecretKeyRingCollection>();

    if (pubRingFound) {
        retval.setFirst(EncryptionUtil.instance().readPublicKeyRingCollection(publicKeyRingFile));
        retval.setSecond(EncryptionUtil.instance().readSecretKeyRingCollection(secretKeyRingFile));
    } else {
        if (publicKeyRingFile.exists() || secretKeyRingFile.exists()) {
            throw new PGPException("Either public or secret keyring not a normal file: " + publicKeyRingFile
                    + ", " + secretKeyRingFile);
        }

        PGPSecretKey key = generateKey(keyDataSource.getIdentity(), keyDataSource.getPassphrase());

        PGPPublicKeyRing publicKeyRing = new PGPPublicKeyRing(key.getPublicKey().getEncoded());
        Collection<PGPPublicKeyRing> collection = Collections.singletonList(publicKeyRing);
        retval.setFirst(new PGPPublicKeyRingCollection(collection));

        PGPSecretKeyRing secretKeyRing = new PGPSecretKeyRing(key.getEncoded());
        Collection<PGPSecretKeyRing> secretKeyRings = Collections.singletonList(secretKeyRing);
        retval.setSecond(new PGPSecretKeyRingCollection(secretKeyRings));

        //TODO bobby save keyrings to the files
    }

    return retval;
}

From source file:com.lyndir.lhunath.opal.crypto.gpg.GPG.java

License:Apache License

/**
 * @param publicKeyFile The file that contains the public key.
 * @param publicKeyId   The ID of the key to retrieve from the file.
 *
 * @return a public key from file./*from ww w  .  j  av a 2  s .  c  o  m*/
 *
 * @throws FileNotFoundException
 * @throws IOException
 * @throws PGPException
 */
@SuppressFBWarnings({ "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE" })
public static PGPPublicKey getPublicKey(final File publicKeyFile, final long publicKeyId)
        throws IOException, PGPException {

    try (FileInputStream publicKeyInputStream = new FileInputStream(publicKeyFile)) {
        return new PGPPublicKeyRing(publicKeyInputStream).getPublicKey(publicKeyId);
    }
}

From source file:de.jtheuer.diki.lib.pgp.PGPHandler.java

License:Open Source License

/**
 * Adds a new public key to the current keyring
 * @param inputstream a BASE64 encoded (armored) PGP Key stream
 * @throws IOException /*from   w  ww.  j  a v a  2 s  .c o m*/
 */
public void addPublicKey(InputStream inputstream) throws IOException {
    PGPPublicKeyRing key = new PGPPublicKeyRing(new ArmoredInputStream(inputstream));
    addPublicKey(key);
}