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

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

Introduction

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

Prototype

public JceOpenSSLPKCS8EncryptorBuilder setRandom(SecureRandom random) 

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.  j  a  v  a 2s  . 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;
}