org.spongycastle.operator.jcajce.JceAsymmetricKeyWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scprov-jdk15 Show documentation
Show all versions of scprov-jdk15 Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle intended for Android.
Android ships with a stripped-down version of Bouncy Castle - this causes classloader collisions if you try to add
an alternative (updated/complete) Bouncy Castle jar.
This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5.
package org.spongycastle.operator.jcajce;
import java.security.GeneralSecurityException;
import java.security.Provider;
import java.security.ProviderException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.crypto.Cipher;
import org.spongycastle.asn1.x509.SubjectPublicKeyInfo;
import org.spongycastle.jcajce.DefaultJcaJceHelper;
import org.spongycastle.jcajce.NamedJcaJceHelper;
import org.spongycastle.jcajce.ProviderJcaJceHelper;
import org.spongycastle.operator.AsymmetricKeyWrapper;
import org.spongycastle.operator.GenericKey;
import org.spongycastle.operator.OperatorException;
public class JceAsymmetricKeyWrapper
extends AsymmetricKeyWrapper
{
private OperatorHelper helper = new OperatorHelper(new DefaultJcaJceHelper());
private PublicKey publicKey;
private SecureRandom random;
public JceAsymmetricKeyWrapper(PublicKey publicKey)
{
super(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()).getAlgorithmId());
this.publicKey = publicKey;
}
public JceAsymmetricKeyWrapper(X509Certificate certificate)
{
this(certificate.getPublicKey());
}
public JceAsymmetricKeyWrapper setProvider(Provider provider)
{
this.helper = new OperatorHelper(new ProviderJcaJceHelper(provider));
return this;
}
public JceAsymmetricKeyWrapper setProvider(String providerName)
{
this.helper = new OperatorHelper(new NamedJcaJceHelper(providerName));
return this;
}
public JceAsymmetricKeyWrapper setSecureRandom(SecureRandom random)
{
this.random = random;
return this;
}
public byte[] generateWrappedKey(GenericKey encryptionKey)
throws OperatorException
{
Cipher keyEncryptionCipher = helper.createAsymmetricWrapper(getAlgorithmIdentifier().getAlgorithm());
byte[] encryptedKeyBytes = null;
try
{
keyEncryptionCipher.init(Cipher.WRAP_MODE, publicKey, random);
encryptedKeyBytes = keyEncryptionCipher.wrap(OperatorUtils.getJceKey(encryptionKey));
}
catch (GeneralSecurityException e)
{
}
catch (IllegalStateException e)
{
}
catch (UnsupportedOperationException e)
{
}
catch (ProviderException e)
{
}
// some providers do not support WRAP (this appears to be only for asymmetric algorithms)
if (encryptedKeyBytes == null)
{
try
{
keyEncryptionCipher.init(Cipher.ENCRYPT_MODE, publicKey, random);
encryptedKeyBytes = keyEncryptionCipher.doFinal(OperatorUtils.getJceKey(encryptionKey).getEncoded());
}
catch (GeneralSecurityException e)
{
throw new OperatorException("unable to encrypt contents key", e);
}
}
return encryptedKeyBytes;
}
}