com.fitbur.bouncycastle.operator.bc.BcAsymmetricKeyWrapper Maven / Gradle / Ivy
package com.fitbur.bouncycastle.operator.bc;
import java.security.SecureRandom;
import com.fitbur.bouncycastle.asn1.ASN1ObjectIdentifier;
import com.fitbur.bouncycastle.asn1.x509.AlgorithmIdentifier;
import com.fitbur.bouncycastle.crypto.AsymmetricBlockCipher;
import com.fitbur.bouncycastle.crypto.CipherParameters;
import com.fitbur.bouncycastle.crypto.InvalidCipherTextException;
import com.fitbur.bouncycastle.crypto.params.AsymmetricKeyParameter;
import com.fitbur.bouncycastle.crypto.params.ParametersWithRandom;
import com.fitbur.bouncycastle.operator.AsymmetricKeyWrapper;
import com.fitbur.bouncycastle.operator.GenericKey;
import com.fitbur.bouncycastle.operator.OperatorException;
public abstract class BcAsymmetricKeyWrapper
extends AsymmetricKeyWrapper
{
private AsymmetricKeyParameter publicKey;
private SecureRandom random;
public BcAsymmetricKeyWrapper(AlgorithmIdentifier encAlgId, AsymmetricKeyParameter publicKey)
{
super(encAlgId);
this.publicKey = publicKey;
}
public BcAsymmetricKeyWrapper setSecureRandom(SecureRandom random)
{
this.random = random;
return this;
}
public byte[] generateWrappedKey(GenericKey encryptionKey)
throws OperatorException
{
AsymmetricBlockCipher keyEncryptionCipher = createAsymmetricWrapper(getAlgorithmIdentifier().getAlgorithm());
CipherParameters params = publicKey;
if (random != null)
{
params = new ParametersWithRandom(params, random);
}
try
{
byte[] keyEnc = OperatorUtils.getKeyBytes(encryptionKey);
keyEncryptionCipher.init(true, publicKey);
return keyEncryptionCipher.processBlock(keyEnc, 0, keyEnc.length);
}
catch (InvalidCipherTextException e)
{
throw new OperatorException("unable to encrypt contents key", e);
}
}
protected abstract AsymmetricBlockCipher createAsymmetricWrapper(ASN1ObjectIdentifier algorithm);
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy