org.spongycastle.jcajce.provider.asymmetric.elgamal.ElGamalUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prov Show documentation
Show all versions of prov Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
The newest version!
package org.spongycastle.jcajce.provider.asymmetric.elgamal;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.interfaces.DHPrivateKey;
import javax.crypto.interfaces.DHPublicKey;
import org.spongycastle.crypto.params.AsymmetricKeyParameter;
import org.spongycastle.crypto.params.ElGamalParameters;
import org.spongycastle.crypto.params.ElGamalPrivateKeyParameters;
import org.spongycastle.crypto.params.ElGamalPublicKeyParameters;
import org.spongycastle.jce.interfaces.ElGamalPrivateKey;
import org.spongycastle.jce.interfaces.ElGamalPublicKey;
/**
* utility class for converting jce/jca ElGamal objects
* objects into their org.spongycastle.crypto counterparts.
*/
public class ElGamalUtil
{
static public AsymmetricKeyParameter generatePublicKeyParameter(
PublicKey key)
throws InvalidKeyException
{
if (key instanceof ElGamalPublicKey)
{
ElGamalPublicKey k = (ElGamalPublicKey)key;
return new ElGamalPublicKeyParameters(k.getY(),
new ElGamalParameters(k.getParameters().getP(), k.getParameters().getG()));
}
else if (key instanceof DHPublicKey)
{
DHPublicKey k = (DHPublicKey)key;
return new ElGamalPublicKeyParameters(k.getY(),
new ElGamalParameters(k.getParams().getP(), k.getParams().getG()));
}
throw new InvalidKeyException("can't identify public key for El Gamal.");
}
static public AsymmetricKeyParameter generatePrivateKeyParameter(
PrivateKey key)
throws InvalidKeyException
{
if (key instanceof ElGamalPrivateKey)
{
ElGamalPrivateKey k = (ElGamalPrivateKey)key;
return new ElGamalPrivateKeyParameters(k.getX(),
new ElGamalParameters(k.getParameters().getP(), k.getParameters().getG()));
}
else if (key instanceof DHPrivateKey)
{
DHPrivateKey k = (DHPrivateKey)key;
return new ElGamalPrivateKeyParameters(k.getX(),
new ElGamalParameters(k.getParams().getP(), k.getParams().getG()));
}
throw new InvalidKeyException("can't identify private key for El Gamal.");
}
}