org.bouncycastle.cert.crmf.bc.BcCRMFEncryptorBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpkix-jdk15on Show documentation
Show all versions of bcpkix-jdk15on Show documentation
The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. This jar contains APIs for JDK 1.5 to JDK 1.8. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.
The newest version!
package org.bouncycastle.cert.crmf.bc;
import java.io.OutputStream;
import java.security.SecureRandom;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.cert.crmf.CRMFException;
import org.bouncycastle.crypto.CipherKeyGenerator;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.util.CipherFactory;
import org.bouncycastle.operator.GenericKey;
import org.bouncycastle.operator.OutputEncryptor;
/**
* Lightweight CRMFOutputEncryptor builder.
*/
public class BcCRMFEncryptorBuilder
{
private final ASN1ObjectIdentifier encryptionOID;
private final int keySize;
private CRMFHelper helper = new CRMFHelper();
private SecureRandom random;
public BcCRMFEncryptorBuilder(ASN1ObjectIdentifier encryptionOID)
{
this(encryptionOID, -1);
}
public BcCRMFEncryptorBuilder(ASN1ObjectIdentifier encryptionOID, int keySize)
{
this.encryptionOID = encryptionOID;
this.keySize = keySize;
}
public BcCRMFEncryptorBuilder setSecureRandom(SecureRandom random)
{
this.random = random;
return this;
}
public OutputEncryptor build()
throws CRMFException
{
return new CRMFOutputEncryptor(encryptionOID, keySize, random);
}
private class CRMFOutputEncryptor
implements OutputEncryptor
{
private KeyParameter encKey;
private AlgorithmIdentifier algorithmIdentifier;
private Object cipher;
CRMFOutputEncryptor(ASN1ObjectIdentifier encryptionOID, int keySize, SecureRandom random)
throws CRMFException
{
random = CryptoServicesRegistrar.getSecureRandom(random);
CipherKeyGenerator keyGen = helper.createKeyGenerator(encryptionOID, random);
encKey = new KeyParameter(keyGen.generateKey());
algorithmIdentifier = helper.generateEncryptionAlgID(encryptionOID, encKey, random);
cipher = helper.createContentCipher(true, encKey, algorithmIdentifier);
}
public AlgorithmIdentifier getAlgorithmIdentifier()
{
return algorithmIdentifier;
}
public OutputStream getOutputStream(OutputStream dOut)
{
return CipherFactory.createOutputStream(dOut, cipher);
}
public GenericKey getKey()
{
return new GenericKey(algorithmIdentifier, encKey.getKey());
}
}
}