Example usage for org.bouncycastle.openssl PKCS8Generator generate

List of usage examples for org.bouncycastle.openssl PKCS8Generator generate

Introduction

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

Prototype

public PemObject generate() throws PemGenerationException 

Source Link

Usage

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

License:Apache License

/**
 * {@inheritDoc}//  w w  w .j  a  v  a 2  s.  co  m
 */
@Override
public String getContent() {
    try {
        PrivateKeyInfo privateKeyInfo = derivePrivateKeyInfo();
        PemObjectBuilder builder = objectBuilderFactory.newBuilder();
        if (passphrase == null) {
            builder.setType("RSA PRIVATE KEY");
            builder.append(privateKeyInfo.getEncoded());
        } else {
            PKCS8Generator generator = new PKCS8Generator(privateKeyInfo, createPrivateKeyEncryptor());
            builder.setType("ENCRYPTED PRIVATE KEY");
            builder.append(generator.generate().getContent());
        }

        return builder.build().getEncoded();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}