org.bouncycastle.openssl.jcajce.JcePEMEncryptorBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpkix-fips Show documentation
Show all versions of bcpkix-fips Show documentation
The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. The APIs are designed primarily to be used in conjunction with the BC FIPS provider. The APIs may also be used with other providers although if being used in a FIPS context it is the responsibility of the user to ensure that any other providers used are FIPS certified.
package org.bouncycastle.openssl.jcajce;
import java.security.Provider;
import java.security.SecureRandom;
import org.bouncycastle.jcajce.util.DefaultJcaJceHelper;
import org.bouncycastle.jcajce.util.JcaJceHelper;
import org.bouncycastle.jcajce.util.NamedJcaJceHelper;
import org.bouncycastle.jcajce.util.ProviderJcaJceHelper;
import org.bouncycastle.openssl.PEMEncryptor;
import org.bouncycastle.openssl.PEMException;
public class JcePEMEncryptorBuilder
{
private final String algorithm;
private JcaJceHelper helper = new DefaultJcaJceHelper();
private SecureRandom random;
public JcePEMEncryptorBuilder(String algorithm)
{
this.algorithm = algorithm;
}
public JcePEMEncryptorBuilder setProvider(Provider provider)
{
this.helper = new ProviderJcaJceHelper(provider);
return this;
}
public JcePEMEncryptorBuilder setProvider(String providerName)
{
this.helper = new NamedJcaJceHelper(providerName);
return this;
}
public JcePEMEncryptorBuilder setSecureRandom(SecureRandom random)
{
this.random = random;
return this;
}
public PEMEncryptor build(final char[] password)
{
if (random == null)
{
random = new SecureRandom();
}
int ivLength = algorithm.startsWith("AES-") ? 16 : 8;
final byte[] iv = new byte[ivLength];
random.nextBytes(iv);
return new PEMEncryptor()
{
public String getAlgorithm()
{
return algorithm;
}
public byte[] getIV()
{
return iv;
}
public byte[] encrypt(byte[] encoding)
throws PEMException
{
return PEMUtilities.crypt(true, helper, encoding, password, algorithm, iv);
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy