List of usage examples for java.security.spec ECPoint ECPoint
public ECPoint(BigInteger x, BigInteger y)
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 w ww.j a va2 s. c o 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 ww .ja 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:org.eclipse.leshan.server.demo.LeshanServerDemo.java
public static void createAndStartServer(int webPort, String localAddress, int localPort, String secureLocalAddress, int secureLocalPort, String redisUrl) throws Exception { // Prepare LWM2M server LeshanServerBuilder builder = new LeshanServerBuilder(); builder.setLocalAddress(localAddress, localPort); builder.setLocalSecureAddress(secureLocalAddress, secureLocalPort); builder.setEncoder(new DefaultLwM2mNodeEncoder()); LwM2mNodeDecoder decoder = new DefaultLwM2mNodeDecoder(); builder.setDecoder(decoder);/* w w w .ja va 2s.c om*/ // connect to redis if needed Pool<Jedis> jedis = null; if (redisUrl != null) { // TODO: support sentinel pool and make pool configurable jedis = new JedisPool(new URI(redisUrl)); } // Get public and private server key PrivateKey privateKey = null; PublicKey publicKey = null; try { // Get point values byte[] publicX = Hex .decodeHex("fcc28728c123b155be410fc1c0651da374fc6ebe7f96606e90d927d188894a73".toCharArray()); byte[] publicY = Hex .decodeHex("d2ffaa73957d76984633fc1cc54d0b763ca0559a9dff9706e9f4557dacc3f52a".toCharArray()); byte[] privateS = Hex .decodeHex("1dae121ba406802ef07c193c1ee4df91115aabd79c1ed7f4c0ef7ef6a5449400".toCharArray()); // Get Elliptic Curve Parameter spec for secp256r1 AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC"); algoParameters.init(new ECGenParameterSpec("secp256r1")); ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class); // Create key specs KeySpec publicKeySpec = new ECPublicKeySpec( new ECPoint(new BigInteger(publicX), new BigInteger(publicY)), parameterSpec); KeySpec privateKeySpec = new ECPrivateKeySpec(new BigInteger(privateS), parameterSpec); // Get keys publicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec); privateKey = KeyFactory.getInstance("EC").generatePrivate(privateKeySpec); builder.setPublicKey(publicKey); builder.setPrivateKey(privateKey); } catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidParameterSpecException e) { LOG.error("Unable to initialize RPK.", e); System.exit(-1); } // Define model provider LwM2mModelProvider modelProvider = new StandardModelProvider(); builder.setObjectModelProvider(modelProvider); // Set securityStore & registrationStore EditableSecurityStore securityStore; if (jedis == null) { // use file persistence securityStore = new FileSecurityStore(); } else { // use Redis Store securityStore = new RedisSecurityStore(jedis); builder.setRegistrationStore(new RedisRegistrationStore(jedis)); } builder.setSecurityStore(securityStore); // Create and start LWM2M server LeshanServer lwServer = builder.build(); // Now prepare Jetty Server server = new Server(webPort); WebAppContext root = new WebAppContext(); root.setContextPath("/"); root.setResourceBase(LeshanServerDemo.class.getClassLoader().getResource("webapp").toExternalForm()); root.setParentLoaderPriority(true); server.setHandler(root); // Create Servlet EventServlet eventServlet = new EventServlet(lwServer, lwServer.getSecureAddress().getPort()); ServletHolder eventServletHolder = new ServletHolder(eventServlet); root.addServlet(eventServletHolder, "/event/*"); ServletHolder clientServletHolder = new ServletHolder( new ClientServlet(lwServer, lwServer.getSecureAddress().getPort())); root.addServlet(clientServletHolder, "/api/clients/*"); ServletHolder securityServletHolder = new ServletHolder(new SecurityServlet(securityStore, publicKey)); root.addServlet(securityServletHolder, "/api/security/*"); ServletHolder objectSpecServletHolder = new ServletHolder( new ObjectSpecServlet(lwServer.getModelProvider())); root.addServlet(objectSpecServletHolder, "/api/objectspecs/*"); // Start Jetty & Leshan lwServer.start(); server.start(); LOG.info("Web server started at {}.", server.getURI()); }
From source file:org.eclipse.leshan.standalone.servlet.json.SecurityDeserializer.java
@Override public SecurityInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json == null) { return null; }// w ww . j a va 2 s .c om SecurityInfo info = null; if (json.isJsonObject()) { JsonObject object = (JsonObject) json; String endpoint = null; 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"); if (psk != null) { // PSK Deserialization String identity = null; 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 (DecoderException e) { throw new JsonParseException(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 (DecoderException | InvalidKeySpecException | NoSuchAlgorithmException | InvalidParameterSpecException e) { throw new JsonParseException("Invalid security info content", e); } info = SecurityInfo.newRawPublicKeyInfo(endpoint, key); } else { throw new JsonParseException("Invalid security info content"); } } return info; }