Example usage for org.bouncycastle.openpgp.bc BcPGPSecretKeyRingCollection BcPGPSecretKeyRingCollection

List of usage examples for org.bouncycastle.openpgp.bc BcPGPSecretKeyRingCollection BcPGPSecretKeyRingCollection

Introduction

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

Prototype

public BcPGPSecretKeyRingCollection(Collection collection) throws IOException, PGPException 

Source Link

Usage

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;
            }//  w ww .  j av a  2s . co m

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

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

            return key;
        }
    }

    return null;
}

From source file:domains.donuts.keyring.TestKeyring.java

License:Open Source License

public TestKeyring() {
    try (InputStream publicInput = PGP_PUBLIC_KEYRING.openStream();
            InputStream privateInput = PGP_PRIVATE_KEYRING.openStream()) {
        publics = new BcPGPPublicKeyRingCollection(PGPUtil.getDecoderStream(publicInput));
        privates = new BcPGPSecretKeyRingCollection(PGPUtil.getDecoderStream(privateInput));
    } catch (PGPException | IOException e) {
        throw new VerifyException("Failed to load PGP keyrings from jar", e);
    }/*from  w  w  w .  jav  a 2 s .  c  om*/
}

From source file:google.registry.keyring.api.DummyKeyringModule.java

License:Open Source License

/** Always returns a {@link InMemoryKeyring} instance. */
@Provides//from www .  j a va2 s .  c  o  m
static Keyring provideKeyring() {
    PGPKeyPair dummyKey;
    try (InputStream publicInput = PGP_PUBLIC_KEYRING.openStream();
            InputStream privateInput = PGP_PRIVATE_KEYRING.openStream()) {
        PGPPublicKeyRingCollection publicKeys = new BcPGPPublicKeyRingCollection(
                PGPUtil.getDecoderStream(publicInput));
        PGPSecretKeyRingCollection privateKeys = new BcPGPSecretKeyRingCollection(
                PGPUtil.getDecoderStream(privateInput));
        dummyKey = lookupKeyPair(publicKeys, privateKeys, EMAIL_ADDRESS, ENCRYPT_SIGN);
    } catch (PGPException | IOException e) {
        throw new VerifyException("Failed to load PGP keys from jar", e);
    }
    // Use the same dummy PGP keypair for all required PGP keys -- a real production system would
    // have different values for these keys.  Pass dummy values for all Strings.
    return new InMemoryKeyring(dummyKey, dummyKey, dummyKey.getPublicKey(), dummyKey, dummyKey.getPublicKey(),
            "not a real key", "not a real key", "not a real password", "not a real login",
            "not a real password", "not a real login", "not a real credential", "not a real key");
}

From source file:google.registry.rde.BouncyCastleTest.java

License:Open Source License

@Test
public void testEncryptDecrypt_KeyRingStyle() throws Exception {
    int bufferSize = 64 * 1024;

    // Alice loads Bob's "publicKey" into memory from her public key ring.
    PGPPublicKeyRingCollection publicKeyRings = new BcPGPPublicKeyRingCollection(
            PGPUtil.getDecoderStream(new ByteArrayInputStream(PUBLIC_KEY)));
    PGPPublicKeyRing publicKeyRing = publicKeyRings.getKeyRings("eric@bouncycastle.org", true, true).next();
    PGPPublicKey publicKey = publicKeyRing.getPublicKey();

    // Alice encrypts the secret message for Bob using his "publicKey".
    PGPEncryptedDataGenerator encryptor = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(AES_128));
    encryptor.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
    byte[] encryptedData;
    try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
        try (OutputStream output2 = encryptor.open(output, new byte[bufferSize])) {
            output2.write(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
        }//from   w w  w  .j  a  v a2s.c  om
        encryptedData = output.toByteArray();
    }
    logger.info("Encrypted data: " + dumpHex(encryptedData));

    // Bob loads his chain of private keys into memory.
    PGPSecretKeyRingCollection privateKeyRings = new BcPGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(new ByteArrayInputStream(PRIVATE_KEY)));

    // Bob decrypt's the OpenPGP message (w/ ciphertext) using his "privateKey".
    try (ByteArrayInputStream input = new ByteArrayInputStream(encryptedData)) {
        PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
        PGPEncryptedDataList encDataList = (PGPEncryptedDataList) pgpFact.nextObject();
        assertThat(encDataList.size()).isEqualTo(1);
        PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) encDataList.get(0);
        // Bob loads the private key to which the message is addressed.
        PGPPrivateKey privateKey = extractPrivateKey(privateKeyRings.getSecretKey(encData.getKeyID()));
        try (InputStream original = encData.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey))) {
            assertThat(CharStreams.toString(new InputStreamReader(original, UTF_8)))
                    .isEqualTo(FALL_OF_HYPERION_A_DREAM);
        }
    }
}

From source file:google.registry.rde.BouncyCastleTest.java

License:Open Source License

@Test
public void testCompressEncryptDecryptDecompress_KeyRingStyle() throws Exception {
    int bufsz = 64 * 1024;

    // Alice loads Bob's "publicKey" into memory from her public key ring.
    PGPPublicKeyRingCollection publicKeyRings = new BcPGPPublicKeyRingCollection(
            PGPUtil.getDecoderStream(new ByteArrayInputStream(PUBLIC_KEY)));
    PGPPublicKeyRing publicKeyRing = publicKeyRings.getKeyRings("eric@bouncycastle.org", true, true).next();
    PGPPublicKey publicKey = publicKeyRing.getPublicKey();

    // Alice encrypts the secret message for Bob using his "publicKey".
    PGPEncryptedDataGenerator encryptor = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(AES_128));
    encryptor.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
    byte[] encryptedData;
    try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
        try (OutputStream output2 = encryptor.open(output, new byte[bufsz])) {
            PGPCompressedDataGenerator kompressor = new PGPCompressedDataGenerator(ZIP);
            try (OutputStream output3 = kompressor.open(output2, new byte[bufsz])) {
                output3.write(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
            }//from w w  w.j a  va  2  s .  c o m
        }
        encryptedData = output.toByteArray();
    }
    logger.info("Encrypted data: " + dumpHex(encryptedData));

    // Bob loads his chain of private keys into memory.
    PGPSecretKeyRingCollection privateKeyRings = new BcPGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(new ByteArrayInputStream(PRIVATE_KEY)));

    // Bob decrypt's the OpenPGP message (w/ ciphertext) using his "privateKey".
    try (ByteArrayInputStream input = new ByteArrayInputStream(encryptedData)) {
        PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
        PGPEncryptedDataList encDataList = (PGPEncryptedDataList) pgpFact.nextObject();
        assertThat(encDataList.size()).isEqualTo(1);
        PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) encDataList.get(0);
        // Bob loads the private key to which the message is addressed.
        PGPPrivateKey privateKey = extractPrivateKey(privateKeyRings.getSecretKey(encData.getKeyID()));
        try (InputStream original = encData.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey))) {
            pgpFact = new BcPGPObjectFactory(original);
            PGPCompressedData kompressedData = (PGPCompressedData) pgpFact.nextObject();
            try (InputStream orig2 = kompressedData.getDataStream()) {
                assertThat(CharStreams.toString(new InputStreamReader(orig2, UTF_8)))
                        .isEqualTo(FALL_OF_HYPERION_A_DREAM);
            }
        }
    }
}

From source file:google.registry.rde.RdeKeyringModule.java

License:Open Source License

@Provides
public Keyring get() {
    PGPPublicKeyRingCollection publics;/*from  www.  java2 s . com*/
    PGPSecretKeyRingCollection privates;
    try (InputStream publicInput = PGP_PUBLIC_KEYRING.openStream();
            InputStream privateInput = PGP_PRIVATE_KEYRING.openStream()) {
        publics = new BcPGPPublicKeyRingCollection(PGPUtil.getDecoderStream(publicInput));
        privates = new BcPGPSecretKeyRingCollection(PGPUtil.getDecoderStream(privateInput));
    } catch (PGPException e) {
        throw new RuntimeException("Failed to load PGP keyrings from jar", e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    final PGPKeyPair rdeStagingKey = PgpHelper.lookupKeyPair(publics, privates, STAGING_KEY_EMAIL, ENCRYPT);
    final PGPKeyPair rdeSigningKey = PgpHelper.lookupKeyPair(publics, privates, SIGNING_KEY_EMAIL, SIGN);
    final PGPPublicKey rdeReceiverKey = PgpHelper.lookupPublicKey(publics, RECEIVER_KEY_EMAIL, ENCRYPT);
    final PGPKeyPair brdaSigningKey = rdeSigningKey;
    final PGPPublicKey brdaReceiverKey = rdeReceiverKey;
    final String sshPublic;
    final String sshPrivate;
    try {
        sshPublic = RdeTestData.loadUtf8("registry-unittest.id_rsa.pub");
        sshPrivate = RdeTestData.loadUtf8("registry-unittest.id_rsa");
    } catch (IOException e) {
        throw new RuntimeException("Failed to load SSH keys from jar", e);
    }
    return new Keyring() {
        @Override
        public PGPPublicKey getRdeStagingEncryptionKey() {
            return rdeStagingKey.getPublicKey();
        }

        @Override
        public PGPPrivateKey getRdeStagingDecryptionKey() {
            return rdeStagingKey.getPrivateKey();
        }

        @Override
        public String getRdeSshClientPublicKey() {
            return sshPublic;
        }

        @Override
        public String getRdeSshClientPrivateKey() {
            return sshPrivate;
        }

        @Override
        public PGPKeyPair getRdeSigningKey() {
            return rdeSigningKey;
        }

        @Override
        public PGPPublicKey getRdeReceiverKey() {
            return rdeReceiverKey;
        }

        @Override
        public String getMarksdbSmdrlLogin() {
            return MARKSDB_SMDRL_LOGIN;
        }

        @Override
        public String getMarksdbLordnPassword() {
            return MARKSDB_LORDN_PASSWORD;
        }

        @Override
        public String getMarksdbDnlLogin() {
            return MARKSDB_DNL_LOGIN;
        }

        @Override
        public String getJsonCredential() {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getIcannReportingPassword() {
            return ICANN_REPORTING_PASSWORD;
        }

        @Override
        public PGPKeyPair getBrdaSigningKey() {
            return brdaSigningKey;
        }

        @Override
        public PGPPublicKey getBrdaReceiverKey() {
            return brdaReceiverKey;
        }

        @Override
        public String getBraintreePrivateKey() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void close() {
        }
    };
}

From source file:google.registry.rde.RdeKeyringModule.java

License:Open Source License

/** Helper method for loading a specific {@link PGPKeyPair}. */
public PGPKeyPair get(String query, KeyRequirement want) {
    PGPPublicKeyRingCollection publics;/*from w  ww.  ja v a  2s . c om*/
    PGPSecretKeyRingCollection privates;
    try (InputStream publicInput = PGP_PUBLIC_KEYRING.openStream();
            InputStream privateInput = PGP_PRIVATE_KEYRING.openStream()) {
        publics = new BcPGPPublicKeyRingCollection(PGPUtil.getDecoderStream(publicInput));
        privates = new BcPGPSecretKeyRingCollection(PGPUtil.getDecoderStream(privateInput));
    } catch (PGPException e) {
        throw new RuntimeException("Failed to load PGP keyrings from jar", e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return PgpHelper.lookupKeyPair(publics, privates, query, want);
}

From source file:google.registry.testing.FakeKeyringModule.java

License:Open Source License

@Provides
public Keyring get() {
    PGPPublicKeyRingCollection publics;//from www. j  a  v a 2s  .  c o  m
    PGPSecretKeyRingCollection privates;
    try (InputStream publicInput = PGP_PUBLIC_KEYRING.openStream();
            InputStream privateInput = PGP_PRIVATE_KEYRING.openStream()) {
        publics = new BcPGPPublicKeyRingCollection(PGPUtil.getDecoderStream(publicInput));
        privates = new BcPGPSecretKeyRingCollection(PGPUtil.getDecoderStream(privateInput));
    } catch (PGPException e) {
        throw new RuntimeException("Failed to load PGP keyrings from jar", e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    final PGPKeyPair rdeStagingKey = PgpHelper.lookupKeyPair(publics, privates, STAGING_KEY_EMAIL, ENCRYPT);
    final PGPKeyPair rdeSigningKey = PgpHelper.lookupKeyPair(publics, privates, SIGNING_KEY_EMAIL, SIGN);
    final PGPPublicKey rdeReceiverKey = PgpHelper.lookupPublicKey(publics, RECEIVER_KEY_EMAIL, ENCRYPT);
    final PGPKeyPair brdaSigningKey = rdeSigningKey;
    final PGPPublicKey brdaReceiverKey = rdeReceiverKey;
    final String sshPublic = readResourceUtf8(FakeKeyringModule.class, "testdata/registry-unittest.id_rsa.pub");
    final String sshPrivate = readResourceUtf8(FakeKeyringModule.class, "testdata/registry-unittest.id_rsa");

    return new Keyring() {
        @Override
        public PGPPublicKey getRdeStagingEncryptionKey() {
            return rdeStagingKey.getPublicKey();
        }

        @Override
        public PGPPrivateKey getRdeStagingDecryptionKey() {
            return rdeStagingKey.getPrivateKey();
        }

        @Override
        public String getRdeSshClientPublicKey() {
            return sshPublic;
        }

        @Override
        public String getRdeSshClientPrivateKey() {
            return sshPrivate;
        }

        @Override
        public PGPKeyPair getRdeSigningKey() {
            return rdeSigningKey;
        }

        @Override
        public PGPPublicKey getRdeReceiverKey() {
            return rdeReceiverKey;
        }

        @Override
        public String getMarksdbSmdrlLogin() {
            return MARKSDB_SMDRL_LOGIN;
        }

        @Override
        public String getMarksdbLordnPassword() {
            return MARKSDB_LORDN_PASSWORD;
        }

        @Override
        public String getMarksdbDnlLogin() {
            return MARKSDB_DNL_LOGIN;
        }

        @Override
        public String getJsonCredential() {
            return JSON_CREDENTIAL;
        }

        @Override
        public String getIcannReportingPassword() {
            return ICANN_REPORTING_PASSWORD;
        }

        @Override
        public PGPKeyPair getBrdaSigningKey() {
            return brdaSigningKey;
        }

        @Override
        public PGPPublicKey getBrdaReceiverKey() {
            return brdaReceiverKey;
        }

        @Override
        public String getBraintreePrivateKey() {
            return BRAINTREE_PRIVATE_KEY;
        }

        @Override
        public void close() {
        }
    };
}

From source file:org.eclipse.packagedrone.repo.signing.pgp.PgpHelper.java

License:Open Source License

public static Stream<PGPKeyRing> streamKeyring(final InputStream input) throws IOException, PGPException {
    final BcPGPSecretKeyRingCollection keyrings = new BcPGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(input));

    final Iterator<?> keyRingIter = keyrings.getKeyRings();

    final Stream<?> s = StreamSupport
            .stream(Spliterators.spliteratorUnknownSize(keyRingIter, Spliterator.ORDERED), false);

    return s.map(o -> (PGPKeyRing) o);
}