Example usage for org.bouncycastle.openssl PKCS8Generator PKCS8Generator

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

Introduction

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

Prototype

public PKCS8Generator(PrivateKeyInfo key, OutputEncryptor outputEncryptor) 

Source Link

Document

Base constructor.

Usage

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

License:Apache License

/**
 * {@inheritDoc}//from ww  w.java2s  .c  o 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);
    }
}