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

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

Introduction

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

Prototype

public PemObject generate() throws PemGenerationException 

Source Link

Usage

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//from  w  w  w  .  ja  va 2 s.c  om
 *            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.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 {// w ww . j  av a 2 s .  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));
    }
}