org.spongycastle.jcajce.provider.asymmetric.util.DHUtil 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.util;
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.DHParameters;
import org.spongycastle.crypto.params.DHPrivateKeyParameters;
import org.spongycastle.crypto.params.DHPublicKeyParameters;
import org.spongycastle.jcajce.provider.asymmetric.dh.BCDHPublicKey;
/**
* utility class for converting jce/jca DH objects
* objects into their org.spongycastle.crypto counterparts.
*/
public class DHUtil
{
static public AsymmetricKeyParameter generatePublicKeyParameter(
PublicKey key)
throws InvalidKeyException
{
if (key instanceof BCDHPublicKey)
{
return ((BCDHPublicKey)key).engineGetKeyParameters();
}
if (key instanceof DHPublicKey)
{
DHPublicKey k = (DHPublicKey)key;
return new DHPublicKeyParameters(k.getY(),
new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
}
throw new InvalidKeyException("can't identify DH public key.");
}
static public AsymmetricKeyParameter generatePrivateKeyParameter(
PrivateKey key)
throws InvalidKeyException
{
if (key instanceof DHPrivateKey)
{
DHPrivateKey k = (DHPrivateKey)key;
return new DHPrivateKeyParameters(k.getX(),
new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
}
throw new InvalidKeyException("can't identify DH private key.");
}
}