Example usage for java.security.spec ECFieldFp getP

List of usage examples for java.security.spec ECFieldFp getP

Introduction

In this page you can find the example usage for java.security.spec ECFieldFp getP.

Prototype

public BigInteger getP() 

Source Link

Document

Returns the prime p of this prime finite field.

Usage

From source file:org.cesecore.keys.util.KeyTools.java

/**
 * Gets the key AlgorithmParameterSpec of supported keys. Can be used to initialize a KeyPairGenerator to generate a key of equal type and size.
 * /*from  www .ja v  a  2  s.c om*/
 * @param pk
 *            PublicKey used to derive the AlgorithmParameterSpec
 * @return null if key is unsupported or pk is null, otherwise a AlgorithmParameterSpec.
 */
public static AlgorithmParameterSpec getKeyGenSpec(final PublicKey pk) {
    if (pk == null) {
        return null;
    }
    AlgorithmParameterSpec ret = null;
    if (pk instanceof RSAPublicKey) {
        log.debug("getKeyGenSpec: RSA");
        final RSAPublicKey rpk = (RSAPublicKey) pk;
        ret = new RSAKeyGenParameterSpec(getKeyLength(pk), rpk.getPublicExponent());
    } else if (pk instanceof DSAPublicKey) {
        log.debug("getKeyGenSpec: DSA");
        final DSAPublicKey dpk = (DSAPublicKey) pk;
        final DSAParams params = dpk.getParams();
        ret = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());
    } else if (pk instanceof ECPublicKey) {
        log.debug("getKeyGenSpec: ECPublicKey");
        final ECPublicKey ecpub = (ECPublicKey) pk;
        final java.security.spec.ECParameterSpec sunsp = ecpub.getParams();
        final EllipticCurve ecurve = new EllipticCurve(sunsp.getCurve().getField(), sunsp.getCurve().getA(),
                sunsp.getCurve().getB());
        // ECParameterSpec par = new ECNamedCurveSpec(null, sunsp.getCurve(), sunsp.getGenerator(), sunsp.getOrder(),
        // BigInteger.valueOf(sunsp.getCofactor()));
        final ECParameterSpec params = new ECParameterSpec(ecurve, sunsp.getGenerator(), sunsp.getOrder(),
                sunsp.getCofactor());
        if (log.isDebugEnabled()) {
            log.debug("Fieldsize: " + params.getCurve().getField().getFieldSize());
            final EllipticCurve curve = params.getCurve();
            log.debug("CurveA: " + curve.getA().toString(16));
            log.debug("CurveB: " + curve.getB().toString(16));
            log.debug("CurveSeed: " + curve.getSeed());
            final ECFieldFp field = (ECFieldFp) curve.getField();
            log.debug("CurveSfield: " + field.getP().toString(16));
            final ECPoint p = params.getGenerator();
            log.debug("Generator: " + p.getAffineX().toString(16) + ", " + p.getAffineY().toString(16));
            log.debug("Order: " + params.getOrder().toString(16));
            log.debug("CoFactor: " + params.getCofactor());
        }
        ret = params;
    } else if (pk instanceof JCEECPublicKey) {
        log.debug("getKeyGenSpec: JCEECPublicKey");
        final JCEECPublicKey ecpub = (JCEECPublicKey) pk;
        final org.bouncycastle.jce.spec.ECParameterSpec bcsp = ecpub.getParameters();
        final ECCurve curve = bcsp.getCurve();
        // TODO: this probably does not work for key generation with the Sun PKCS#11 provider. Maybe seed needs to be set to null as above? Or
        // something else, the BC curve is it the same?
        final ECParameterSpec params = new ECNamedCurveSpec(null, curve, bcsp.getG(), bcsp.getN(), bcsp.getH());
        ret = params;
        // EllipticCurve ecc = new EllipticCurve(curve.)
        // ECParameterSpec sp = new ECParameterSpec(, bcsp.getG(), bcsp.getN(), bcsp.getH().intValue());
    }
    return ret;
}

From source file:org.ejbca.util.keystore.KeyTools.java

/**
 * Gets the key AlgorithmParameterSpec of supported keys. Can be used to initialize a KeyPairGenerator to generate a key of equal type and size.
 * @param pk PublicKey used to derive the AlgorithmParameterSpec
 * @return null if key is unsupported or pk is null, otherwise a AlgorithmParameterSpec.
 *///  w w  w. ja  va 2  s.co m
public static AlgorithmParameterSpec getKeyGenSpec(final PublicKey pk) {
    if (pk == null) {
        return null;
    }
    AlgorithmParameterSpec ret = null;
    if (pk instanceof RSAPublicKey) {
        log.debug("getKeyGenSpec: RSA");
        final RSAPublicKey rpk = (RSAPublicKey) pk;
        ret = new RSAKeyGenParameterSpec(getKeyLength(pk), rpk.getPublicExponent());
    } else if (pk instanceof DSAPublicKey) {
        log.debug("getKeyGenSpec: DSA");
        final DSAPublicKey dpk = (DSAPublicKey) pk;
        final DSAParams params = dpk.getParams();
        ret = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());
    } else if (pk instanceof ECPublicKey) {
        log.debug("getKeyGenSpec: ECPublicKey");
        final ECPublicKey ecpub = (ECPublicKey) pk;
        final java.security.spec.ECParameterSpec sunsp = ecpub.getParams();
        final EllipticCurve ecurve = new EllipticCurve(sunsp.getCurve().getField(), sunsp.getCurve().getA(),
                sunsp.getCurve().getB());
        //ECParameterSpec par = new ECNamedCurveSpec(null, sunsp.getCurve(), sunsp.getGenerator(), sunsp.getOrder(), BigInteger.valueOf(sunsp.getCofactor()));
        final ECParameterSpec params = new ECParameterSpec(ecurve, sunsp.getGenerator(), sunsp.getOrder(),
                sunsp.getCofactor());
        if (log.isDebugEnabled()) {
            log.debug("Fieldsize: " + params.getCurve().getField().getFieldSize());
            final EllipticCurve curve = params.getCurve();
            log.debug("CurveA: " + curve.getA().toString(16));
            log.debug("CurveB: " + curve.getB().toString(16));
            log.debug("CurveSeed: " + curve.getSeed());
            final ECFieldFp field = (ECFieldFp) curve.getField();
            log.debug("CurveSfield: " + field.getP().toString(16));
            final ECPoint p = params.getGenerator();
            log.debug("Generator: " + p.getAffineX().toString(16) + ", " + p.getAffineY().toString(16));
            log.debug("Order: " + params.getOrder().toString(16));
            log.debug("CoFactor: " + params.getCofactor());
        }
        ret = params;
    } else if (pk instanceof JCEECPublicKey) {
        log.debug("getKeyGenSpec: JCEECPublicKey");
        final JCEECPublicKey ecpub = (JCEECPublicKey) pk;
        final org.bouncycastle.jce.spec.ECParameterSpec bcsp = ecpub.getParameters();
        final ECCurve curve = bcsp.getCurve();
        //TODO: this probably does not work for key generation with the Sun PKCS#11 provider. Maybe seed needs to be set to null as above? Or something else, the BC curve is it the same?
        final ECParameterSpec params = new ECNamedCurveSpec(null, curve, bcsp.getG(), bcsp.getN(), bcsp.getH());
        ret = params;
        //EllipticCurve ecc = new EllipticCurve(curve.)
        //ECParameterSpec sp = new ECParameterSpec(, bcsp.getG(), bcsp.getN(), bcsp.getH().intValue());
    }
    return ret;
}