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

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

Introduction

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

Prototype

public JceOpenSSLPKCS8EncryptorBuilder(ASN1ObjectIdentifier algorithm) 

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  ww .  ja  va2 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./*w w w . j  a  va  2 s. com*/
 * @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.soulwing.credo.service.crypto.bc.BcPKCS8EncryptionService.java

License:Apache License

private OutputEncryptor createPrivateKeyEncryptor(char[] passphrase) {
    try {/*from   w w w. j  av a  2s.  c o  m*/
        return new JceOpenSSLPKCS8EncryptorBuilder(PKCS8Generator.PBE_SHA1_3DES).setPasssword(passphrase)
                .setIterationCount(65536).build();
    } catch (OperatorCreationException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.soulwing.credo.service.crypto.bc.BcPrivateKeyWrapper.java

License:Apache License

private OutputEncryptor createPrivateKeyEncryptor() {
    try {//  w  w w  .ja v a  2  s .  com
        Validate.notNull(passphrase, "passphrase is required");
        return new JceOpenSSLPKCS8EncryptorBuilder(PKCS8Generator.PBE_SHA1_3DES).setPasssword(passphrase)
                .setIterationCount(100).build();
    } catch (OperatorCreationException ex) {
        throw new RuntimeException(ex);
    }
}