Example usage for java.security.spec ECGenParameterSpec ECGenParameterSpec

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

Introduction

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

Prototype

public ECGenParameterSpec(String stdName) 

Source Link

Document

Creates a parameter specification for EC parameter generation using a standard (or predefined) name stdName in order to generate the corresponding (precomputed) elliptic curve domain parameters.

Usage

From source file:com.aaasec.sigserv.cssigapp.KeyStoreFactory.java

private static KeyPair generateECDSAKeyPair()
        throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
    ECGenParameterSpec ecSpec = new ECGenParameterSpec("P-256");

    KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
    g.initialize(ecSpec, new SecureRandom());
    KeyPair pair = g.generateKeyPair();
    return pair;// w  w w.ja v a2  s . c o  m
}

From source file:net.jmhertlein.core.crypto.Keys.java

/**
 * Generates a new Elliptic Curve Digital Signature Algorithm (ECDSA) public/private key pair.
 *
 * System's default SecureRandom is used
 * @param curveName the name of a pre-defined elliptic curve (e.g. secp521r1)
 * @param provider the JCE provider to use
 * @return a new ECDSA key pair//  w  ww.j av a  2s. c o  m
 */
public static KeyPair newECDSAKeyPair(String curveName, String provider) {
    KeyPair ret;
    try {
        ECGenParameterSpec ecGenSpec = new ECGenParameterSpec(curveName);
        KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", provider);
        g.initialize(ecGenSpec, new SecureRandom());
        ret = g.generateKeyPair();
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | NoSuchProviderException ex) {
        Logger.getLogger(Keys.class.getName()).log(Level.SEVERE, null, ex);
        ret = null;
    }

    return ret;
}

From source file:net.jmhertlein.mcanalytics.api.auth.SSLUtil.java

/**
 * Generates a new Elliptic Curve Digital Signature Algorithm (ECDSA) public/private key pair.
 *
 * System's default SecureRandom is used
 *
 * @param curveName the name of a pre-defined elliptic curve (e.g. secp521r1)
 * @param provider the JCE provider to use
 * @return a new ECDSA key pair/*from   w  w  w  .j a  va2s .  c  o  m*/
 */
public static KeyPair newECDSAKeyPair(String curveName, String provider) {
    KeyPair ret;
    try {
        ECGenParameterSpec ecGenSpec = new ECGenParameterSpec(curveName);
        KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", provider);
        g.initialize(ecGenSpec, new SecureRandom());
        ret = g.generateKeyPair();
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | NoSuchProviderException ex) {
        Logger.getLogger(SSLUtil.class.getName()).log(Level.SEVERE, null, ex);
        ex.printStackTrace(System.err);
        ret = null;
    }

    return ret;
}

From source file:ie.peternagy.jcrypto.algo.EllipticCurveWrapper.java

/**
 * Generate a set of Elliptic Curve keys
 *///from   w w  w . j  av a  2s  .c om
public void generateKeys() {
    tryLoadKeys();
    if (isInitialized(true) && isInitialized(false)) {
        return;
    }

    try {
        ECGenParameterSpec ecGenSpec = new ECGenParameterSpec(EC_CURVE);
        KeyPairGenerator g = KeyPairGenerator.getInstance(ALGORITHM_NAME);
        g.initialize(ecGenSpec, CryptoSecurityUtil.getSecureRandom());
        KeyPair pair = g.generateKeyPair();
        privateKey = pair.getPrivate();
        publicKey = pair.getPublic();

        writeKeys();
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException ex) {
        Logger.getLogger(EllipticCurveWrapper.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.trsst.Common.java

static final KeyPair generateSigningKeyPair() {
    try {//from   w ww  . j  a  va  2 s. co  m
        KeyPairGenerator kpg;
        // kpg = KeyPairGenerator.getInstance("EC", "BC");
        kpg = new org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi.EC();
        kpg.initialize(new ECGenParameterSpec(CURVE_NAME));
        KeyPair kp = kpg.generateKeyPair();
        return kp;
        // } catch (NoSuchAlgorithmException e) {
        // log.error("Error while generating key: " + e.getMessage(), e);
        // } catch (NoSuchProviderException e) {
        // log.error("Error while generating key: " + e.getMessage(), e);
    } catch (InvalidAlgorithmParameterException e) {
        log.error("Error while generating key: " + e.getMessage(), e);
    }
    return null;
}

From source file:com.trsst.Common.java

static final KeyPair generateEncryptionKeyPair() {
    try {//from   ww w.  j  ava 2s  .c  o  m
        KeyPairGenerator kpg;
        // kpg = KeyPairGenerator.getInstance("EC", "BC");
        kpg = new org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi.EC();
        kpg.initialize(new ECGenParameterSpec(CURVE_NAME));
        KeyPair kp = kpg.generateKeyPair();
        return kp;
        // } catch (NoSuchAlgorithmException e) {
        // log.error("Error while generating key: " + e.getMessage(), e);
        // } catch (NoSuchProviderException e) {
        // e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:net.maritimecloud.identityregistry.utils.CertificateUtil.java

/**
 * Generates a keypair (public and private) based on Elliptic curves.
 * /* ww  w . ja v  a  2s. c o m*/
 * @return The generated keypair
 */
public static KeyPair generateKeyPair() {
    ECGenParameterSpec ecGenSpec = new ECGenParameterSpec("secp384r1");
    KeyPairGenerator g;
    try {
        g = KeyPairGenerator.getInstance("ECDSA", BC_PROVIDER_NAME);
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    try {
        g.initialize(ecGenSpec, new SecureRandom());
    } catch (InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    KeyPair pair = g.generateKeyPair();
    return pair;
}

From source file:org.cesecore.certificates.util.AlgorithmTools.java

/** Check if the curve name is known by the first found PKCS#11 provider or default (if none was found)*/
public static boolean isNamedECKnownInDefaultProvider(String ecNamedCurveBc) {
    final Provider[] providers = Security.getProviders("KeyPairGenerator.EC");
    String providerName = providers[0].getName();
    try {//from   ww  w  . j a  v a2  s  . co  m
        for (Provider ecProvider : providers) {
            //This will list something like: SunPKCS11-NSS, BC, SunPKCS11-<library>-slot<slotnumber>
            if (log.isDebugEnabled()) {
                log.debug("Found EC capable provider named: " + ecProvider.getName());
            }
            if (ecProvider.getName().startsWith("SunPKCS11-")
                    && !ecProvider.getName().startsWith("SunPKCS11-NSS")) {
                providerName = ecProvider.getName();
                break;
            }
        }
        final KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", providerName);
        kpg.initialize(new ECGenParameterSpec(ecNamedCurveBc));
        return true;
    } catch (InvalidAlgorithmParameterException e) {
        if (log.isDebugEnabled()) {
            log.debug(ecNamedCurveBc + " is not available in provider " + providerName);
        }
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(
                "EC capable provider " + providerName + " could no longer handle elliptic curve algorithm..",
                e);
    } catch (NoSuchProviderException e) {
        throw new RuntimeException("EC capable provider " + providerName + " disappeard unexpectedly.", e);
    }
    return false;
}

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

private void generateEC(final String name, final String keyEntryName)
        throws InvalidAlgorithmParameterException {
    if (log.isTraceEnabled()) {
        log.trace(">generate EC: curve name " + name + ", keyEntryName " + keyEntryName);
    }//  ww  w. j a  v a 2s .co  m
    // Generate the EC Keypair
    KeyPairGenerator kpg;
    try {
        kpg = KeyPairGenerator.getInstance("EC", this.providerName);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Algorithm " + "EC" + "was not recognized.", e);
    } catch (NoSuchProviderException e) {
        throw new IllegalStateException("BouncyCastle was not found as a provider.", e);
    }
    try {
        Provider prov = Security.getProvider(this.providerName);
        if (StringUtils.contains(prov.getClass().getName(), "iaik")) {
            throw new InvalidAlgorithmParameterException("IAIK ECC key generation not implemented.");
            /*
            ECDSAPrivateKey privateKeyTemplate = new ECDSAPrivateKey();
            privateKeyTemplate.getSign().setBooleanValue(Boolean.TRUE);
            privateKeyTemplate.getToken().setBooleanValue(Boolean.FALSE);
                    
            ECDSAPublicKey publicKeyTemplate = new ECDSAPublicKey();
            publicKeyTemplate.getVerify().setBooleanValue(Boolean.TRUE);
            publicKeyTemplate.getToken().setBooleanValue(Boolean.FALSE);
                    
            ObjectID eccCurveObjectID = new ObjectID(objectID);
            publicKeyTemplate.getEcdsaParams().setByteArrayValue(DerCoder.encode(eccCurveObjectID));
                    
            PKCS11KeyPairGenerationSpec keyPairGenerationSpec =
               new PKCS11KeyPairGenerationSpec(tokenManager, publicKeyTemplate, privateKeyTemplate, 
               PKCS11Spec.USE_READ_WRITE_SESSION, PKCS11Spec.USE_USER_SESSION);
                    
            keyPairGenerator.initialize(keyPairGenerationSpec);
            */
        } else {
            ECGenParameterSpec ecSpec = new ECGenParameterSpec(name);
            if (StringUtils.equals(name, "implicitlyCA")) {
                log.debug("Generating implicitlyCA encoded ECDSA key pair");
                // If the keySpec is null, we have "implicitlyCA" defined EC parameters
                // The parameters were already installed when we installed the provider
                // We just make sure that ecSpec == null here
                ecSpec = null;
            }
            kpg.initialize(ecSpec);
        }
    } catch (InvalidAlgorithmParameterException e) {
        log.debug("EC name " + name + " not supported.");
        throw e;
    }
    generateKeyPair(kpg, keyEntryName, "SHA1withECDSA");
    if (log.isTraceEnabled()) {
        log.trace("<generate: curve name " + name + ", keyEntryName " + keyEntryName);
    }
}

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

private void generateExtraEC(final String name, final String keyEntryName, final String algInstanceName,
        final String sigAlgName) throws InvalidAlgorithmParameterException {
    if (log.isTraceEnabled()) {
        log.trace(">generate " + algInstanceName + ": curve name " + name + ", keyEntryName " + keyEntryName);
    }/* w w  w .  j av a  2s  .  co m*/
    // Generate the EC Keypair
    KeyPairGenerator kpg;
    try {
        kpg = KeyPairGenerator.getInstance(algInstanceName, this.providerName);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Algorithm " + name + "was not recognized.", e);
    } catch (NoSuchProviderException e) {
        throw new IllegalStateException("BouncyCastle was not found as a provider.", e);
    }
    try {
        ECGenParameterSpec ecSpec = new ECGenParameterSpec(name);
        kpg.initialize(ecSpec);
    } catch (InvalidAlgorithmParameterException e) {
        log.debug("EC " + algInstanceName + " name " + name + " not supported.");
        throw e;
    }
    generateKeyPair(kpg, keyEntryName, sigAlgName);
    if (log.isTraceEnabled()) {
        log.trace("<generate: curve name " + name + ", keyEntryName " + keyEntryName);
    }
}