Example usage for org.bouncycastle.openssl.jcajce JcaPKCS8Generator JcaPKCS8Generator

List of usage examples for org.bouncycastle.openssl.jcajce JcaPKCS8Generator JcaPKCS8Generator

Introduction

In this page you can find the example usage for org.bouncycastle.openssl.jcajce JcaPKCS8Generator JcaPKCS8Generator.

Prototype

public JcaPKCS8Generator(PrivateKey key, OutputEncryptor encryptor) throws PemGenerationException 

Source Link

Usage

From source file:craterdog.security.RsaCertificateManager.java

License:Open Source License

@Override
public String encodePrivateKey(PrivateKey key, char[] password) {
    logger.entry();//from w  ww  .j a  v  a 2s  .c  om
    try (StringWriter swriter = new StringWriter(); PemWriter pwriter = new PemWriter(swriter)) {
        OutputEncryptor encryptor = new JcePKCSPBEOutputEncryptorBuilder(NISTObjectIdentifiers.id_aes128_CBC)
                .setProvider(PROVIDER_NAME).build(password);
        PKCS8Generator generator = new JcaPKCS8Generator(key, encryptor);
        pwriter.writeObject(generator);
        pwriter.flush();
        String result = swriter.toString();
        logger.exit();
        return result;
    } catch (IOException | OperatorCreationException e) {
        RuntimeException exception = new RuntimeException(
                "An unexpected exception occurred while attempting to encode a private key.", e);
        throw logger.throwing(exception);
    }
}

From source file:org.albertschmitt.crypto.RSAService.java

License:Open Source License

/**
 * Encrypt the KeyPair with the password and return it as a PEM object.
 *
 * @param keyPair/*  w  w  w .j  ava2 s.  c o  m*/
 *            The RSA Private / Public Key Pair.
 * @param password
 *            The RSA Private Key will be encrypted with this password.
 * @return A PEM object with the encrypted KeyPair..
 * @throws OperatorCreationException
 * @throws PemGenerationException
 */
private PemObject encryptKey(KeyPair keyPair, char[] password)
        throws OperatorCreationException, PemGenerationException {
    final JceOpenSSLPKCS8EncryptorBuilder encryptorBuilder = new JceOpenSSLPKCS8EncryptorBuilder(
            PKCS8Generator.PBE_SHA1_3DES);
    encryptorBuilder.setRandom(new SecureRandom());
    encryptorBuilder.setPasssword(password);
    encryptorBuilder.setIterationCount(10000);
    OutputEncryptor oe = encryptorBuilder.build();
    final JcaPKCS8Generator gen = new JcaPKCS8Generator(keyPair.getPrivate(), oe);
    final PemObject pem = gen.generate();
    return pem;
}

From source file:org.apache.zookeeper.common.X509TestHelpers.java

License:Apache License

/**
 * PEM-encodes the given private key (compatible with OpenSSL), optionally protecting it with a password, and
 * returns the result as a String.//from w  w  w.j  a v  a  2 s  . co  m
 * @param key the private key.
 * @param password an optional key password. If empty or null, the private key will not be encrypted.
 * @return a String containing the PEM encoding of the private key.
 * @throws IOException if converting the key to PEM format fails.
 * @throws OperatorCreationException if constructing the encryptor from the given password fails.
 */
public static String pemEncodePrivateKey(PrivateKey key, String password)
        throws IOException, OperatorCreationException {
    StringWriter stringWriter = new StringWriter();
    JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter);
    OutputEncryptor encryptor = null;
    if (password != null && password.length() > 0) {
        encryptor = new JceOpenSSLPKCS8EncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC)
                .setProvider(BouncyCastleProvider.PROVIDER_NAME).setRandom(PRNG)
                .setPasssword(password.toCharArray()).build();
    }
    pemWriter.writeObject(new JcaPKCS8Generator(key, encryptor));
    pemWriter.close();
    return stringWriter.toString();
}

From source file:org.curioswitch.common.server.framework.armeria.SslContextKeyConverter.java

License:Open Source License

public static SslContextBuilder execute(InputStream keyCertChainFile, InputStream keyFile,
        BiFunction<InputStream, InputStream, SslContextBuilder> operation) {
    final byte[] key;
    final byte[] keyCertChain;
    try {//  ww w  . j a  v a2s .co  m
        key = ByteStreams.toByteArray(keyFile);
        keyCertChain = ByteStreams.toByteArray(keyCertChainFile);
    } catch (IOException e) {
        throw new UncheckedIOException("Could not read file to bytes.", e);
    }

    try {
        return operation.apply(new ByteArrayInputStream(keyCertChain), new ByteArrayInputStream(key));
    } catch (Exception e) {
        // Try to convert the key to PCKS8.
        PrivateKey privateKey = KeyUtil.loadPrivateKey(key);
        final PemObject encoded;
        try {
            JcaPKCS8Generator generator = new JcaPKCS8Generator(privateKey, null);
            encoded = generator.generate();
        } catch (PemGenerationException ex) {
            throw new IllegalStateException("Could not generate PKCS8", ex);
        }

        StringWriter sw = new StringWriter();
        try (JcaPEMWriter pw = new JcaPEMWriter(sw)) {
            pw.writeObject(encoded);
        } catch (IOException ex) {
            throw new UncheckedIOException("Could not write key to String, can't happen.", ex);
        }
        byte[] pkcs8key = sw.toString().getBytes(StandardCharsets.UTF_8);
        return operation.apply(new ByteArrayInputStream(keyCertChain), new ByteArrayInputStream(pkcs8key));
    }
}