Search in sources :

Example 41 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project xipki by xipki.

the class CaClientExample method generateEcKeypair.

protected static MyKeypair generateEcKeypair() throws GeneralSecurityException {
    KeyPairGenerator kpGen = KeyPairGenerator.getInstance("EC");
    ECGenParameterSpec spec = new ECGenParameterSpec("secp256r1");
    kpGen.initialize(spec);
    KeyPair kp = kpGen.generateKeyPair();
    ECPublicKey pub = (ECPublicKey) kp.getPublic();
    byte[] keyData = new byte[65];
    keyData[0] = 4;
    copyArray(pub.getW().getAffineX().toByteArray(), keyData, 1, 32);
    copyArray(pub.getW().getAffineY().toByteArray(), keyData, 33, 32);
    AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, SECObjectIdentifiers.secp256r1);
    SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(algId, keyData);
    return new MyKeypair(kp.getPrivate(), subjectPublicKeyInfo);
}
Also used : KeyPair(java.security.KeyPair) ECPublicKey(java.security.interfaces.ECPublicKey) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 42 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project fitpay-android-sdk by fitpay.

the class KeysManager method createECCKeyPair.

// Create the public and private keys
private ECCKeyPair createECCKeyPair() throws Exception {
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(ALGORITHM, SecurityProvider.getInstance().getProvider());
    keyGenerator.initialize(new ECGenParameterSpec(EC_CURVE), new SecureRandom());
    KeyPair keyPair = keyGenerator.generateKeyPair();
    ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
    ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
    ECCKeyPair eccKeyPair = new ECCKeyPair();
    eccKeyPair.setKeyId(UUID.randomUUID().toString());
    eccKeyPair.setPrivateKey(Hex.bytesToHexString(privateKey.getEncoded()));
    eccKeyPair.setPublicKey(Hex.bytesToHexString(publicKey.getEncoded()));
    return eccKeyPair;
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair) ECPublicKey(java.security.interfaces.ECPublicKey) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair)

Example 43 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project leshan by eclipse.

the class SecurityDeserializer method deserialize.

@Override
public SecurityInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json == null) {
        return null;
    }
    SecurityInfo info = null;
    if (json.isJsonObject()) {
        JsonObject object = (JsonObject) json;
        String endpoint;
        if (object.has("endpoint")) {
            endpoint = object.get("endpoint").getAsString();
        } else {
            throw new JsonParseException("Missing endpoint");
        }
        JsonObject psk = (JsonObject) object.get("psk");
        JsonObject rpk = (JsonObject) object.get("rpk");
        JsonPrimitive x509 = object.getAsJsonPrimitive("x509");
        if (psk != null) {
            // PSK Deserialization
            String identity;
            if (psk.has("identity")) {
                identity = psk.get("identity").getAsString();
            } else {
                throw new JsonParseException("Missing PSK identity");
            }
            byte[] key;
            try {
                key = Hex.decodeHex(psk.get("key").getAsString().toCharArray());
            } catch (IllegalArgumentException e) {
                throw new JsonParseException("key parameter must be a valid hex string", e);
            }
            info = SecurityInfo.newPreSharedKeyInfo(endpoint, identity, key);
        } else if (rpk != null) {
            PublicKey key;
            try {
                byte[] x = Hex.decodeHex(rpk.get("x").getAsString().toCharArray());
                byte[] y = Hex.decodeHex(rpk.get("y").getAsString().toCharArray());
                String params = rpk.get("params").getAsString();
                AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
                algoParameters.init(new ECGenParameterSpec(params));
                ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
                KeySpec keySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)), parameterSpec);
                key = KeyFactory.getInstance("EC").generatePublic(keySpec);
            } catch (IllegalArgumentException | InvalidKeySpecException | NoSuchAlgorithmException | InvalidParameterSpecException e) {
                throw new JsonParseException("Invalid security info content", e);
            }
            info = SecurityInfo.newRawPublicKeyInfo(endpoint, key);
        } else if (x509 != null && x509.getAsBoolean()) {
            info = SecurityInfo.newX509CertInfo(endpoint);
        } else {
            throw new JsonParseException("Invalid security info content");
        }
    }
    return info;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) PublicKey(java.security.PublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo) AlgorithmParameters(java.security.AlgorithmParameters)

Example 44 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project web3sdk by FISCO-BCOS.

the class Keys method createSecp256k1KeyPair.

static KeyPair createSecp256k1KeyPair(SecureRandom random) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDSA", "BC");
    ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256k1");
    if (random != null) {
        keyPairGenerator.initialize(ecGenParameterSpec, random);
    } else {
        keyPairGenerator.initialize(ecGenParameterSpec);
    }
    return keyPairGenerator.generateKeyPair();
}
Also used : ECGenParameterSpec(java.security.spec.ECGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator)

Example 45 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project zookeeper by apache.

the class X509TestHelpers method generateECKeyPair.

/**
 * Generates an elliptic curve key pair using the "secp256r1" aka "prime256v1" aka "NIST P-256" curve.
 * @return the key pair.
 */
public static KeyPair generateECKeyPair() throws GeneralSecurityException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
    keyGen.initialize(new ECGenParameterSpec(DEFAULT_ELLIPTIC_CURVE_NAME), PRNG);
    return keyGen.generateKeyPair();
}
Also used : ECGenParameterSpec(java.security.spec.ECGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator)

Aggregations

ECGenParameterSpec (java.security.spec.ECGenParameterSpec)67 KeyPairGenerator (java.security.KeyPairGenerator)31 AlgorithmParameters (java.security.AlgorithmParameters)23 KeyPair (java.security.KeyPair)23 BigInteger (java.math.BigInteger)19 PublicKey (java.security.PublicKey)19 ECPublicKey (java.security.interfaces.ECPublicKey)17 ECParameterSpec (java.security.spec.ECParameterSpec)15 ECPoint (java.security.spec.ECPoint)15 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)13 Test (org.junit.Test)13 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 KeySpec (java.security.spec.KeySpec)11 ECPrivateKey (java.security.interfaces.ECPrivateKey)9 PrivateKey (java.security.PrivateKey)8 SecureRandom (java.security.SecureRandom)8 Cipher (javax.crypto.Cipher)8 GeneralSecurityException (java.security.GeneralSecurityException)7 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)7