Example usage for java.security.spec EllipticCurve EllipticCurve

List of usage examples for java.security.spec EllipticCurve EllipticCurve

Introduction

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

Prototype

public EllipticCurve(ECField field, BigInteger a, BigInteger b) 

Source Link

Document

Creates an elliptic curve with the specified elliptic field field and the coefficients a and b .

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDH", "BC");
    EllipticCurve curve = new EllipticCurve(
            new ECFieldFp(new BigInteger("fffffffffffffffffffffffffffffffeffffffffffffffff", 16)),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16));

    ECParameterSpec ecSpec = new ECParameterSpec(curve,
            new ECPoint(new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
                    new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16)),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), 1);

    keyGen.initialize(ecSpec, new SecureRandom());

    KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
    KeyPair aPair = keyGen.generateKeyPair();
    KeyAgreement bKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
    KeyPair bPair = keyGen.generateKeyPair();

    aKeyAgree.init(aPair.getPrivate());/*from  ww  w . j a va  2  s  .co m*/
    bKeyAgree.init(bPair.getPrivate());

    aKeyAgree.doPhase(bPair.getPublic(), true);
    bKeyAgree.doPhase(aPair.getPublic(), true);

    MessageDigest hash = MessageDigest.getInstance("SHA1", "BC");

    System.out.println(new String(hash.digest(aKeyAgree.generateSecret())));
    System.out.println(new String(hash.digest(bKeyAgree.generateSecret())));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDH", "BC");
    EllipticCurve curve = new EllipticCurve(
            new ECFieldFp(new BigInteger("fffffffffffffffffffffffffffffffeffffffffffffffff", 16)),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16));

    ECParameterSpec ecSpec = new ECParameterSpec(curve,
            new ECPoint(new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
                    new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16)),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), 1);

    keyGen.initialize(ecSpec, new SecureRandom());

    KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
    KeyPair aPair = keyGen.generateKeyPair();
    KeyAgreement bKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
    KeyPair bPair = keyGen.generateKeyPair();

    aKeyAgree.init(aPair.getPrivate());//w w  w.java 2 s . co m
    bKeyAgree.init(bPair.getPrivate());

    aKeyAgree.doPhase(bPair.getPublic(), true);
    bKeyAgree.doPhase(aPair.getPublic(), true);

    MessageDigest hash = MessageDigest.getInstance("SHA1", "BC");

    System.out.println(new String(hash.digest(aKeyAgree.generateSecret())));
    System.out.println(new String(hash.digest(bKeyAgree.generateSecret())));
}

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.
 * /*  w  w w. j a  v a  2 s. c o m*/
 * @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.
 *///ww  w  .j  av a  2  s  .  c  om
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;
}