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);
}
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;
}
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;
}
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();
}
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();
}
Aggregations