Example usage for org.bouncycastle.openpgp PGPSecretKeyRing getSecretKeys

List of usage examples for org.bouncycastle.openpgp PGPSecretKeyRing getSecretKeys

Introduction

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

Prototype

public Iterator<PGPSecretKey> getSecretKeys() 

Source Link

Document

Return an iterator containing all the secret keys.

Usage

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

License:Apache License

/**
 * Gets the correct signing key from local secret keyring using the supplied
 * key information.// w w w. j a  v  a 2 s.  co  m
 * 
 * @param keyInfo
 *            the supplied key information
 * @return the correct signing key
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws PGPException
 *             thrown if an error is encountered
 */
public PGPSecretKey getSignKey(final String keyInfo) throws IOException, PGPException {
    final PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(new FileInputStream(this.secretKeyRing)));

    final Iterator keyRingIter = pgpSec.getKeyRings();
    while (keyRingIter.hasNext()) {
        final PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();

        final Iterator keyIter = keyRing.getSecretKeys();
        while (keyIter.hasNext()) {
            final PGPSecretKey key = (PGPSecretKey) keyIter.next();

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

        }
    }

    return null;
}

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

public static PGPSecretKey findSecretKey(InputStream privateKeyIn) throws PGPException, IOException {

    //TODO FIXME/*  w  ww. ja  va2 s.c o m*/
    InputStream in = PGPUtil.getDecoderStream(privateKeyIn);
    KeyFingerPrintCalculator fingerPrintCalculator = null;

    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in, fingerPrintCalculator);

    for (Iterator<PGPSecretKeyRing> iter = pgpSec.getKeyRings(); iter.hasNext();) {
        PGPSecretKeyRing pgpSecretKeyRing = iter.next();
        for (Iterator<PGPSecretKey> keysIter = pgpSecretKeyRing.getSecretKeys(); keysIter.hasNext();) {
            PGPSecretKey secretKey = keysIter.next();
            return secretKey;
        }
    }

    throw new NoSuchElementException("No private key found.");
}

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

License:Open Source License

/**
 * A simple routine that opens a key ring file and finds the first available
 * key suitable for signature generation.
 * /*from ww  w  . ja v  a  2s  .  c o m*/
 * @param in
 * @return
 * @throws IOException
 * @throws PGPException
 */
private static PGPSecretKey findSigningKey(PGPSecretKeyRingCollection secRing)
        throws IOException, PGPException {
    //
    // We just loop through the collection till we find a key suitable for encryption.
    //
    PGPSecretKey key = null;

    @SuppressWarnings("unchecked")
    Iterator<PGPSecretKeyRing> rIt = secRing.getKeyRings();

    while (key == null && rIt.hasNext()) {
        PGPSecretKeyRing kRing = rIt.next();
        @SuppressWarnings("unchecked")
        Iterator<PGPSecretKey> kIt = kRing.getSecretKeys();

        while (key == null && kIt.hasNext()) {
            PGPSecretKey k = (PGPSecretKey) kIt.next();
            if (k.isSigningKey()) {
                key = k;
            }
        }
    }

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

    return key;
}

From source file:com.github.sannies.nexusaptplugin.sign.PGPSigner.java

License:Apache License

/**
 * Returns the secret key matching the specified identifier.
 * //from   w  ww .j a va2  s . c o m
 * @param input the input stream containing the keyring collection
 * @param keyId the 4 bytes identifier of the key
 */
private PGPSecretKey getSecretKey(InputStream input, String keyId) throws IOException, PGPException {
    PGPSecretKeyRingCollection keyrings = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));

    Iterator rIt = keyrings.getKeyRings();

    while (rIt.hasNext()) {
        PGPSecretKeyRing kRing = (PGPSecretKeyRing) rIt.next();
        Iterator kIt = kRing.getSecretKeys();

        while (kIt.hasNext()) {
            PGPSecretKey key = (PGPSecretKey) kIt.next();

            if (key.isSigningKey()
                    && Long.toHexString(key.getKeyID() & 0xFFFFFFFFL).equals(keyId.toLowerCase())) {
                return key;
            }
        }
    }

    return null;
}

From source file:com.google.e2e.bcdriver.Decryptor.java

License:Apache License

static final PGPPrivateKey extractDecryptionKey(PGPSecretKeyRing pskr, String pass) throws PGPException {
    Iterator<PGPSecretKey> skit = Util.getTypedIterator(pskr.getSecretKeys(), PGPSecretKey.class);

    PGPSecretKey selected = null;/*from   w  w w.  java2  s .  co m*/

    // Pass #1 - use key flags on signatures.
    while (skit.hasNext()) {
        PGPSecretKey sk = skit.next();
        Iterator<PGPSignature> sigit = Util.getTypedIterator(sk.getPublicKey().getSignatures(),
                PGPSignature.class);
        while (sigit.hasNext()) {
            if (Util.hasKeyFlag(sigit.next(), KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) {
                selected = sk;
                break;
            }
        }
    }
    if (selected == null) {
        // Pass #2 - use intrinsic key capabilities, but prefer subkeys
        // where possible.
        skit = Util.getTypedIterator(pskr.getSecretKeys(), PGPSecretKey.class);
        while (skit.hasNext()) {
            PGPSecretKey sk = skit.next();
            if (sk.getPublicKey().isEncryptionKey()) {
                selected = sk;
                // But continue the loop, so subkeys will be chosen.
            }
        }
    }

    if (selected != null) {
        return selected
                .extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
                        .build(pass.toCharArray()));
    } else {
        return null;
    }
}

From source file:com.navnorth.learningregistry.LRSigner.java

License:Apache License

/**
 * Reads private key from the provided InputStream
 *
 * @param input InputStream of the private key
 * @return PGPSecretKey/*from www  .j a v a 2s  . com*/
 * @throws LRException NO_KEY error if the key cannot be obtained from the input stream
 */
private PGPSecretKey readSecretKey(InputStream input) throws LRException {
    PGPSecretKeyRingCollection pgpSec;

    try {
        pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));
    } catch (Exception e) {
        throw new LRException(LRException.NO_KEY);
    }

    java.util.Iterator keyRingIter = pgpSec.getKeyRings();
    while (keyRingIter.hasNext()) {
        PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();

        java.util.Iterator keyIter = keyRing.getSecretKeys();
        while (keyIter.hasNext()) {
            PGPSecretKey key = (PGPSecretKey) keyIter.next();

            if (key.isSigningKey()) {
                return key;
            }
        }
    }

    throw new LRException(LRException.NO_KEY);
}

From source file:com.verhas.licensor.License.java

License:Open Source License

/**
 * Load the secret key to be used to encrypt the license. After the key is
 * loaded it can be used to encrypt license files.
 * /*from  ww  w.j  a va 2s.c om*/
 * @param in
 *            input stream of the file containing the key rings
 * @param userId
 *            the user id of the key. If this parameter is {@code null} then
 *            the first key on the key ring appropriate to sign will be
 *            used.
 * @throws java.io.IOException
 * @throws org.bouncycastle.openpgp.PGPException
 */
@SuppressWarnings("unchecked")
public License loadKey(InputStream in, final String userId) throws IOException, PGPException {
    in = PGPUtil.getDecoderStream(in);

    final PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in);
    key = null;
    for (final PGPSecretKeyRing kRing : in((Iterator<PGPSecretKeyRing>) pgpSec.getKeyRings())) {
        for (final PGPSecretKey k : in((Iterator<PGPSecretKey>) kRing.getSecretKeys())) {
            for (final String keyUserId : in((Iterator<String>) k.getUserIDs())) {
                if (keyIsAppropriate(userId, keyUserId, k)) {
                    key = k;
                    return this;
                }
            }
        }
    }

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

From source file:com.zwitserloot.ivyplusplus.mavencentral.CreateDetachedSignatures_.java

License:Open Source License

PGPSecretKey getSigningKey(InputStream keyData, String streamName)
        throws IOException, PGPException, SigningException {
    PGPSecretKeyRingCollection keyrings_ = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyData));
    Iterator<?> keyrings = keyrings_.getKeyRings();
    while (keyrings.hasNext()) {
        PGPSecretKeyRing keys_ = (PGPSecretKeyRing) keyrings.next();
        Iterator<?> keys = keys_.getSecretKeys();
        while (keys.hasNext()) {
            PGPSecretKey key = (PGPSecretKey) keys.next();
            if (key.isSigningKey())
                return key;
        }/*from   w ww .  ja va 2 s  . c o  m*/
    }

    throw new SigningException("No signing key found in keyring: " + streamName);
}

From source file:crypttools.PGPCryptoBC.java

License:Open Source License

/**
* <p>Return the first suitable key for encryption in the key ring
* collection. For this case we only expect there to be one key
* available for signing.</p>// w  w  w.  ja  v  a2  s .co  m
* 
* @param input - the input stream of the key PGP Key Ring
* @return the first suitable PGP Secret Key found for signing
* @throws IOException
* @throws PGPException
*/
@SuppressWarnings("unchecked")
private static PGPSecretKey readSecretKey(InputStream input) throws IOException, PGPException {
    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));
    Iterator<PGPSecretKeyRing> iter = pgpSec.getKeyRings();
    PGPSecretKey secKey = null;

    while (iter.hasNext() && secKey == null) {
        PGPSecretKeyRing keyRing = iter.next();
        Iterator<PGPSecretKey> keyIter = keyRing.getSecretKeys();

        while (keyIter.hasNext()) {
            PGPSecretKey key = keyIter.next();
            if (key.isSigningKey()) {
                secKey = key;
                break;
            }
        }
    }

    if (secKey != null) {
        return secKey;
    } else {
        throw new IllegalArgumentException("Can't find signing key in key ring.");
    }
}

From source file:de.dentrassi.pm.signing.pgp.PgpHelper.java

License:Open Source License

public static PGPSecretKey loadSecretKey(final InputStream input, final String keyId)
        throws IOException, PGPException {
    final long keyIdNum = Long.parseUnsignedLong(keyId, 16);

    final BcPGPSecretKeyRingCollection keyrings = new BcPGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(input));

    final Iterator<?> keyRingIter = keyrings.getKeyRings();
    while (keyRingIter.hasNext()) {
        final PGPSecretKeyRing secretKeyRing = (PGPSecretKeyRing) keyRingIter.next();

        final Iterator<?> secretKeyIterator = secretKeyRing.getSecretKeys();
        while (secretKeyIterator.hasNext()) {
            final PGPSecretKey key = (PGPSecretKey) secretKeyIterator.next();

            if (!key.isSigningKey()) {
                continue;
            }/*from w  ww  .ja v a2s  .  c  o m*/

            final long shortId = key.getKeyID() & 0xFFFFFFFFL;

            if (key.getKeyID() != keyIdNum && shortId != keyIdNum) {
                continue;
            }

            return key;
        }
    }

    return null;
}