Example usage for org.bouncycastle.openpgp.operator.jcajce JcaPGPDigestCalculatorProviderBuilder JcaPGPDigestCalculatorProviderBuilder

List of usage examples for org.bouncycastle.openpgp.operator.jcajce JcaPGPDigestCalculatorProviderBuilder JcaPGPDigestCalculatorProviderBuilder

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp.operator.jcajce JcaPGPDigestCalculatorProviderBuilder JcaPGPDigestCalculatorProviderBuilder.

Prototype

public JcaPGPDigestCalculatorProviderBuilder() 

Source Link

Document

Default constructor.

Usage

From source file:com.bekwam.resignator.util.CryptUtils.java

License:Apache License

private byte[] decrypt(byte[] encrypted, char[] passPhrase)
        throws IOException, PGPException, NoSuchProviderException {
    try (InputStream in = new ByteArrayInputStream(encrypted)) {
        InputStream decoderIn = PGPUtil.getDecoderStream(in);

        PGPObjectFactory pgpF = new PGPObjectFactory(decoderIn, new BcKeyFingerprintCalculator());
        PGPEncryptedDataList enc;//  ww  w. j  a  va2  s  . c  om
        Object o = pgpF.nextObject();

        if (o == null) { // decryption failed; there is no next object

            //
            // This could arise if there is a problem with the underlying file.
            //

            if (logger.isWarnEnabled()) {
                logger.warn(
                        "Field could not be decrypted. (Config file modified outside of app?)  Returning input bytes as encrypted bytes.");
            }

            return encrypted;
        }

        //
        // the first object might be a PGP marker packet.
        //

        if (o instanceof PGPEncryptedDataList) {
            enc = (PGPEncryptedDataList) o;
        } else {
            enc = (PGPEncryptedDataList) pgpF.nextObject(); // i don't think this will be used
        }

        PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);

        InputStream clear = pbe.getDataStream(new JcePBEDataDecryptorFactoryBuilder(
                new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC")
                        .build(passPhrase));

        return Streams.readAll(clear);
    }
}

From source file:com.fuzion.tools.pgp.BCPGPKeyGenTools.java

License:Open Source License

/**
 * //  www .j  av a 2 s.c o m
 * @param dsaKeyPair - the generated DSA key pair
 * @param elGamalKeyPair - the generated El Gamal key pair
 * @param identity - the given identity of the key pair ring
 * @param passphrase - the secret pass phrase to protect the key pair
 * @return a PGP Key Ring Generate with the El Gamal key pair added as sub key
 * @throws Exception
 */
@SuppressWarnings("deprecation")
public static final PGPKeyRingGenerator createPGPKeyRingGeneratorForDSAKeyPair(KeyPair dsaKeyPair,
        KeyPair elGamalKeyPair, String identity, char[] passphrase) throws Exception {
    PGPKeyPair dsaPgpKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKeyPair, new Date());
    PGPKeyPair elGamalPgpKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elGamalKeyPair, new Date());
    PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build()
            .get(HashAlgorithmTags.SHA1);
    PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaPgpKeyPair,
            identity, sha1Calc, null, null,
            new JcaPGPContentSignerBuilder(dsaPgpKeyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1),
            new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc).setProvider("BC")
                    .build(passphrase));

    keyRingGen.addSubKey(elGamalPgpKeyPair);
    return keyRingGen;
}

From source file:com.fuzion.tools.pgp.BCPGPKeyGenTools.java

License:Open Source License

/**
 * /*from   w  ww. j  av  a  2  s.  c om*/
 * @param signKeyPair - the generated signing RSA key pair
 * @param encryptKeyPair - the generated encrypting RSA key pair
 * @param identity - the given identity of the key pair ring
 * @param passphrase - the secret pass phrase to protect the key pair
 * @return a PGP Key Ring Generate with the RSA key pair added as sub key
 * @throws Exception
 */
@SuppressWarnings("deprecation")
public static final PGPKeyRingGenerator createPGPKeyRingGeneratorForRSAKeyPair(KeyPair signKeyPair,
        KeyPair encryptKeyPair, String identity, char[] passphrase) throws Exception {
    PGPKeyPair signPgpKeyPair = new PGPKeyPair(PGPPublicKey.RSA_SIGN, signKeyPair, new Date());
    PGPKeyPair encryptPgpKeyPair = new PGPKeyPair(PGPPublicKey.RSA_ENCRYPT, encryptKeyPair, new Date());
    PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build()
            .get(HashAlgorithmTags.SHA1);
    PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION,
            signPgpKeyPair, identity, sha1Calc, null, null,
            new JcaPGPContentSignerBuilder(signPgpKeyPair.getPublicKey().getAlgorithm(),
                    HashAlgorithmTags.SHA1),
            new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc).setProvider("BC")
                    .build(passphrase));

    keyRingGen.addSubKey(encryptPgpKeyPair);
    return keyRingGen;
}

From source file:crypttools.PGPTools.java

License:Open Source License

/**
 * //from  w  w w .  ja  va  2  s .  co  m
 * @param dsaKeyPair - the generated DSA key pair
 * @param elGamalKeyPair - the generated El Gamal key pair
 * @param identity - the given identity of the key pair ring
 * @param passphrase - the secret pass phrase to protect the key pair
 * @return a PGP Key Ring Generate with the El Gamal key pair added as sub key
 * @throws Exception
 */
@SuppressWarnings("deprecation")
public static final PGPKeyRingGenerator createPGPKeyRingGenerator(KeyPair dsaKeyPair, KeyPair elGamalKeyPair,
        String identity, char[] passphrase) throws Exception {
    PGPKeyPair dsaPgpKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKeyPair, new Date());
    PGPKeyPair elGamalPgpKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elGamalKeyPair, new Date());
    PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build()
            .get(HashAlgorithmTags.SHA1);

    PGPContentSignerBuilder pgpCSB = new JcaPGPContentSignerBuilder(dsaPgpKeyPair.getPublicKey().getAlgorithm(),
            HashAlgorithmTags.SHA1);
    PBESecretKeyEncryptor pbeSKE = new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc)
            .setProvider("BC").build(passphrase);

    PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaPgpKeyPair,
            identity, sha1Calc, null, null, pgpCSB, pbeSKE);

    keyRingGen.addSubKey(elGamalPgpKeyPair);
    return keyRingGen;
}

From source file:gobblin.crypto.GPGFileDecryptor.java

License:Apache License

public static InputStream decryptFile(InputStream inputStream, String passPhrase) throws IOException {

    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        Security.addProvider(new BouncyCastleProvider());
    }/*  ww w  .  j av a2  s .co  m*/
    inputStream = PGPUtil.getDecoderStream(inputStream);

    JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(inputStream);
    PGPEncryptedDataList enc;
    Object pgpfObject = pgpF.nextObject();

    if (pgpfObject instanceof PGPEncryptedDataList) {
        enc = (PGPEncryptedDataList) pgpfObject;
    } else {
        enc = (PGPEncryptedDataList) pgpF.nextObject();
    }

    PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);

    InputStream clear;
    try {
        clear = pbe
                .getDataStream(new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder()
                        .setProvider(BouncyCastleProvider.PROVIDER_NAME).build())
                                .setProvider(BouncyCastleProvider.PROVIDER_NAME)
                                .build(passPhrase.toCharArray()));

        JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear);
        pgpfObject = pgpFact.nextObject();
        if (pgpfObject instanceof PGPCompressedData) {
            PGPCompressedData cData = (PGPCompressedData) pgpfObject;
            pgpFact = new JcaPGPObjectFactory(cData.getDataStream());
            pgpfObject = pgpFact.nextObject();
        }

        PGPLiteralData ld = (PGPLiteralData) pgpfObject;
        return ld.getInputStream();
    } catch (PGPException e) {
        throw new IOException(e);
    }
}

From source file:gobblin.util.GPGFileDecrypter.java

License:Open Source License

public static FSDataInputStream decryptFile(InputStream inputStream, String passPhrase) throws IOException {

    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        Security.addProvider(new BouncyCastleProvider());
    }/*from   w ww  . j a v a 2  s .  c om*/
    inputStream = PGPUtil.getDecoderStream(inputStream);

    JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(inputStream);
    PGPEncryptedDataList enc;
    Object pgpfObject = pgpF.nextObject();

    if (pgpfObject instanceof PGPEncryptedDataList) {
        enc = (PGPEncryptedDataList) pgpfObject;
    } else {
        enc = (PGPEncryptedDataList) pgpF.nextObject();
    }

    PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);

    InputStream clear;
    try {
        clear = pbe
                .getDataStream(new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder()
                        .setProvider(BouncyCastleProvider.PROVIDER_NAME).build())
                                .setProvider(BouncyCastleProvider.PROVIDER_NAME)
                                .build(passPhrase.toCharArray()));

        JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear);
        pgpfObject = pgpFact.nextObject();
        if (pgpfObject instanceof PGPCompressedData) {
            PGPCompressedData cData = (PGPCompressedData) pgpfObject;
            pgpFact = new JcaPGPObjectFactory(cData.getDataStream());
            pgpfObject = pgpFact.nextObject();
        }

        PGPLiteralData ld = (PGPLiteralData) pgpfObject;
        return StreamUtils.convertStream(ld.getInputStream());
    } catch (PGPException e) {
        throw new IOException(e);
    }
}

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

License:Open Source License

/**
 * Serialize a PGPKeyPair//from   ww  w.j  a  va2 s  .  co  m
 *
 * <p>Use this to serialize a PGPPrivateKey as well (pairing it with the corresponding
 * PGPPublicKey), as private keys can't be serialized on their own.
 */
public static byte[] serializeKeyPair(PGPKeyPair keyPair) throws IOException, PGPException {
    try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
        // NOTE: We have to close the ArmoredOutputStream before calling the underlying OutputStream's
        // "toByteArray". Failing to do so would result in a truncated serialization as we took the
        // byte array before the ArmoredOutputStream wrote all the data.
        //
        // Even "flushing" the ArmoredOutputStream isn't enough - as there are parts that are only
        // written by the ArmoredOutputStream when it is closed: the "-----END PGP PRIVATE KEY
        // BLOCK-----" (or similar) footer.
        try (ArmoredOutputStream out = new ArmoredOutputStream(byteStream)) {
            new PGPSecretKey(keyPair.getPrivateKey(), keyPair.getPublicKey(),
                    new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()
                            .get(HashAlgorithmTags.SHA256),
                    true, null).encode(out);
        }
        return byteStream.toByteArray();
    }
}

From source file:org.apache.gobblin.crypto.GPGFileDecryptor.java

License:Apache License

/**
 * Taking in a file inputstream and a passPhrase, generate a decrypted file inputstream.
 * @param inputStream file inputstream// w  w w.j  ava  2s .  c o  m
 * @param passPhrase passPhrase
 * @return
 * @throws IOException
 */
public InputStream decryptFile(InputStream inputStream, String passPhrase) throws IOException {

    PGPEncryptedDataList enc = getPGPEncryptedDataList(inputStream);
    PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);
    InputStream clear;

    try {
        clear = pbe
                .getDataStream(new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder()
                        .setProvider(BouncyCastleProvider.PROVIDER_NAME).build())
                                .setProvider(BouncyCastleProvider.PROVIDER_NAME)
                                .build(passPhrase.toCharArray()));

        JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear);

        return new LazyMaterializeDecryptorInputStream(pgpFact);
    } catch (PGPException e) {
        throw new IOException(e);
    }
}

From source file:org.elasticsearch.plugins.InstallPluginCommandTests.java

License:Apache License

public PGPSecretKey newSecretKey() throws NoSuchAlgorithmException, NoSuchProviderException, PGPException {
    final KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(2048);//  www.j  a va 2  s.co m
    final KeyPair pair = kpg.generateKeyPair();
    final PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build()
            .get(HashAlgorithmTags.SHA1);
    final PGPKeyPair pkp = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, pair, new Date());
    return new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, pkp, "example@example.com", sha1Calc, null,
            null, new JcaPGPContentSignerBuilder(pkp.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1),
            new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5, sha1Calc)
                    .setProvider(new BouncyCastleProvider()).build("passphrase".toCharArray()));
}

From source file:org.elasticsearch.plugins.InstallPluginCommandTests.java

License:Apache License

private String signature(final byte[] bytes, final PGPSecretKey secretKey) {
    try {//from w  w w . j  av a2  s.co m
        final PGPPrivateKey privateKey = secretKey.extractPrivateKey(
                new BcPBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().build())
                        .build("passphrase".toCharArray()));
        final PGPSignatureGenerator generator = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(
                privateKey.getPublicKeyPacket().getAlgorithm(), HashAlgorithmTags.SHA512));
        generator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        try (BCPGOutputStream pout = new BCPGOutputStream(new ArmoredOutputStream(output));
                InputStream is = new ByteArrayInputStream(bytes)) {
            final byte[] buffer = new byte[1024];
            int read;
            while ((read = is.read(buffer)) != -1) {
                generator.update(buffer, 0, read);
            }
            generator.generate().encode(pout);
        }
        return new String(output.toByteArray(), "UTF-8");
    } catch (IOException | PGPException e) {
        throw new RuntimeException(e);
    }
}